在运行时动态/惰性地导入python模块。还支持易于使用的导入路径名。
dynamic-import的Python项目详细描述
允许您在运行时动态/惰性地导入python模块,启用易于使用的导入路径名,节省内存另外,不必担心嵌套的文件夹结构会产生长的导入名称,从而使管理项目变得容易。
警告
`importer()`参数在版本“0.9.4”中已更改无需手动提供`__package__`name。将在将来的版本中引发DeprecationWarning
安装、更新和卸载
使用pip来:
pip install dynamic-import pip install --upgrade dynamic-import pip uninstall dynamic-import
示例
/example/sample/初始化py
fromdynamic_importimportimporter# Static/Normal Importfrom.staticimportstatic# Dynamic Importerimporter({'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.py
# Static Import #1# ----------------fromsampleimportstatic# Only "sample" & "sample.static" modules are loaded at this point.print(static())print()# Dynamic Import #1# -----------------fromsampleimporta,b,c# Now "sample", "sample.static" & "sample.one" modules are loaded at this point.print(a())print(b())print(c())print()# Dynamic Import #2# -----------------fromsampleimportx,y,z# All "sample", "sample.static", "sample.one" & "sample.two" modules are loaded.print(x())print(y())print(z())print()# Dynamic Import #3# -----------------fromsampleimportinternals# This is to demonstrate you can relatively import one module from another module.print(internals())print()# Sub-page Import #1# ------------------fromsampleimporte,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# ------------------fromsampleimportname# Another sub-dir exampleprint(name())print()
注
- you can still use static/normal import e.g. ^{tt3}$ before importer() is called.
- You can also use . e.g. ^{tt4}$
- for 1 word import name you can use ^{tt5}$ vs ^{tt6}$
- All import names must be unique.
许可证
自由,没有任何限制。Read more