Package: dynamic-import
Dynamic ImportLet Dynamic Import handle your projects(package) import needs. Enables you to dynamically(lazily) import module as needed within the project and for external usage on run-time.Placeimporter()into top__init__.pyfile and forget about where variable, function, class, … are located.Just callfrom <package> import <variable>while coding and when end-user calls from your project. All names are accessible at top level, easy to remember.Move/rename file within your project? No problem, auto updates.Supports.pyand.so(experimental)Saves memory.Ultimate worry free management comfort as your project grows from small to large.RequiresPython 3.8+Linux, MacOSInstall, update & uninstallUsepipto:python3-mpipinstalldynamic-import# installpython3-mpipinstall--upgradedynamic-import# updatepython3-mpipuninstalldynamic-import# uninstallInstall directly from GitHubpython3-mpipinstall--upgradegit+https://github.com/YoSTEALTH/Dynamic-ImportUsage# ./<package>/__init__.pyfromdynamic_importimportimporterimporter()importer()optionsimporter(cache=False)# disable & remove cache file (for temporary use only!)importer(recursive=False)# do not scan sub-directories# exclude `.py, .so` fileimporter(exclude_file='file.py')# singleimporter(exclude_file=('one.py','sub-dir/two.py',...))# multiple# exclude sub-directoryimporter(exclude_dir='sub-dir')# singleimporter(exclude_dir=('sub-dir','sub/sub-dir',...))# multipleExample# ./example/pkg/__init__.pyfromdynamic_importimportimporter__version__='1.2.3'# if need to use special name place it above `importer()`importer()# only need to call `importer()` once inside main `__init__.py` file.# note: `importer()` will scan all package directory and sub-directories for `.py, .so`# files and cache import names for dynamic use.# ./example/pkg/var.pyimportsys# just like normal import if `__all__` is not defined, `my_var` will be included.# Also `sys` will not be included.my_var=sys.version_info.major# ./example/pkg/functions/myfunction.pyfrompkgimportmy_var# all import names are available at higher level,# no need for `from ..example.var import my_var`__all__='my_function'# using just string for single name is okdefmy_function():returnmy_var+1# ./example/classes/__init__.py__all__=['MyClass']classMyClass:passCalling# ./example/calling.pyfrompkgimportmy_var,my_function,MyClass# import all 3 names regardless of where module is located# orimportpkgMyClass()print(my_var,pkg.my_varismy_var)# 3 Trueprint(my_function())# 4print(dir(pkg))# ['my_var', 'my_function', 'MyClass', ...]NoteOnly need to callimporter()once inside__init__.pyfile.All sub-directories will be scanned for.py, .sofile asrecursive=Trueby default.Useexclude_dirto list sub-directories you would like to avoid scanning.You can still use normal static/relative import.For one word import name you can use string e.g.__all__ = 'function'vs__all__ =('function',)All import names must be unique.Cache can be disabled & removed by usingimporter(cache=False)Cached temporary files are stored in./__pycache__/__init__.importer-<python-version>.pycYou can move or rename any.pyfile within project directory or sub-directory and import will not break.Special name e.g:__something__are ignored. If need to use special name place it
aboveimporter()e.g:__version__ = '1.2.3'; importer()Usingfrom <package> import *is not recommended unless you want to load all the modules.No need to have empty__init__.pyinside sub-directories. Namespace + Package combined into one.Callingdir(<package>)enables you to show all importable names without actually loading modules.Having module name and function name the same is ok! e.g.from pkg import my_funwhile./pkg/my_fun.pyand callingmy_fun()will not conflict with module name. Module will still load in the background.Experimentalimporter()also works with certain.cpython-<...>.so.abi3.sofile (tested with cython created.so)..soshould not contain any function/class that auto-run on import, e.g:run_something()VisitLiburingto see project using Dynamic Import with.sofiles in action.LicenseFree, Public Domain (CC0).Read more