Skip to main content

Dynamically/Lazily import python module on run-time. Also enables easy to use import path name.

Project description

Lets you dynamically/lazily import python module on run-time, enables easy to use import path name, saves memory. Also makes managing projects easy by not having to worry about nested folder structure producing long import names.

Install, update & uninstall

Use pip to:

pip install dynamic-import

pip install --upgrade dynamic-import

pip uninstall dynamic-import

Example

./example/sample/__init__.py

from dynamic_import import importer

# Static/Normal Import
from .static import static

# Dynamic Importer
importer(
    {
        'one': ('a', 'b', 'c'),  # from .one import a, b, c
        'two': ('x', 'y', 'z'),  # from .two import x, y, z
        'local': 'internals',    # from .local import internals
        'sub': {
            'page': ('e', 'f', 'g'),  # from .sub.page import e, f, g
            'name': 'name',           # from .sub.name import name
        }
    }
)

./example/example.py

# Static Import #1
# ----------------
from sample import static
# Only "sample" & "sample.static" modules are loaded at this point.
print(static())
print()

# Dynamic Import #1
# -----------------
from sample import a, b, c
# Now "sample", "sample.static" & "sample.one" modules are loaded at this point.
print(a())
print(b())
print(c())
print()

# Dynamic Import #2
# -----------------
from sample import x, y, z
# All "sample", "sample.static", "sample.one" & "sample.two" modules are loaded.
print(x())
print(y())
print(z())
print()

# Dynamic Import #3
# -----------------
from sample import internals
# This is to demonstrate you can relatively import one module from another module.
print(internals())
print()

# Sub-page Import #1
# ------------------
from sample import e, f, g
# This demonstrates you can use nested sub-dir and use main module to import from.
print(e())
print(f())
print(g())
print()

# Sub-page Import #2
# ------------------
from sample import name
# Another sub-dir example
print(name())
print()

Note

  • you can still use static/normal import e.g. from .module import example before importer() is called.
  • You can also use . e.g. '.one': ('a', 'b', 'c')
  • for 1 word import name you can use 'module': 'myclass' vs 'module': ('myclass',)
  • All import names must be unique.

License

Free, No limit what so ever. Read more

TODO

  • Raise warning if conflicting module.identifier name is found (currently old is overridden)

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 dynamic-import, version 2020.2.10
Filename, size File type Python version Upload date Hashes
Filename, size dynamic_import-2020.2.10.tar.gz (5.1 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page