在运行时动态/惰性地导入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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
默认情况下,JavaApachePOI锁定所有单元格。如何使所有单元格解锁?   java Play框架:为什么https url在使用“sbt dist”命令时不起作用?   ldap查询搜索筛选器中“&”的java意义   java构造不带警告的ImmutableSortedSet   用户界面Java GUI图形有多高效?   java在整个应用程序中使用相同的数据还是复制它?   java如何为所有打开的文档应用插入键操作   java存储setter方法的路径,以便以后可以调用它   java加载具有相同密钥的外部属性文件   Java通过inputmap跟踪击键   Java SSLSocket错误:无法从非静态上下文引用   java访问名为的文件的正确方式   java左括号打印在后缀中&右括号错误   java在循环中对齐数字列