Skip to main content

Import non-python files

Project description


An ext-tensible loader to import anything like it is a python module.

Supports Python 3.6+.


pip install import-x


Example json file in your path foo.json:

        "why": "not",
   # Extend the ExtensionLoader and implement 'handle_module' method
   # where you will get a module object and the path to that module.

   >>> from import_x import ExtensionLoader

   >>> class JsonLoader(ExtensionLoader):
        extension = '.json'

        auto_enable = False

        def handle_module(module, path):
            Load the json file and set as `data` attribute of the module.
            json_file = Path(path)
            content = json_file.read_text()
                data = json.loads(content)
            except (json.JSONDecodeError, ValueError):
                data = {}
   = data

    >>> json_imports = JsonLoader()
    >>> with json_imports:
            import foo
    >>> {"why": "not"}

If you want to enable imports automatically without the context_manager then just do auto_enable = True in your loader.

This Example JsonLoader can be used directly by importing

    from import_x.loaders.json_loader import JsonLoader

and you are ready to import all the json files.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for import-x, version 0.1.1
Filename, size File type Python version Upload date Hashes
Filename, size import_x-0.1.1-py3-none-any.whl (4.3 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size import-x-0.1.1.tar.gz (3.9 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page