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


热门话题
java使用McClickListener单击了什么元素   Java时间戳在Oracle时间戳中不同情况下存储12 PM的奇怪行为   java无法使用事件总线对运行在不同机器上的垂直体进行通信   java Mockserver:收到请求后进行回调   java无法将Json字符串转换为Map<string,Object>   java如何按升序排列输出?   java视图行,带有oracle键。jbo。在SrCategoryParentIterator中找不到键[300100120394155]   javafxmysql连接示例   java正在等待加载完成   java是否可以将同一个有状态会话bean实例注入多个其他会话bean?   java无法让万向节检测离开或进入区域   使用JavaCV和OpenCV的java提供了dyld:lazy符号绑定失败:找不到符号:__sincos_stret   xml解析无法使用Java读取xml文档   java无法更改工具栏的颜色   javaapachesshd和JSCH   java无法在firebase存储中检索图像url   java问题与executeUpdate   同一应用程序中不同活动之间的java SharedReference