python模块的独立导入
localimport的Python项目详细描述
localimport允许您在独立的 环境,保护全球进口国。
功能
- 模拟python模块导入的隔离环境
- 计算*.pth个文件
- 与pkg_resources命名空间兼容
- mockspkgutil.extend_path()支持压缩的python鸡蛋
示例
给定python脚本,应用程序或插件会附带一个目录 如果包含要导入的模块,则可以使用localimport保存 全球进口商国家清洁。
app.py res/modules/ some_package/ __init__.py
# app.pywithlocalimport('res/modules')as_importer:importsome_packageassert'some_package'notinsys.modules**Important**:Youmustkeepthereferencetothe``localimport``objectalive,especiallyifyouuse``fromxximportyy``imports.
用法
可以在下面找到localimport的预缩版本 Gist。你当然可以缩小 您自己编写的代码,例如使用 nr命令行工具。
nr py.blob localimport.py -cme localimport > localimport-gzb64-w80.py
根据应用程序的不同,您可能需要使用引导程序条目 要点
# @@@ minified localimport here @@@withlocalimport('.')as_importer:frommy_application_package.__main__importmainmain()
API
localimport(path, parent_dir=None, do_eggs=True, do_pth=True, do_autodisable=True)
A context manager that creates an isolated environment for importing Python modules. Once the context manager exits, the previous global state is restored.
Note that the context can be entered multiple times, but it is not recommended generally as the only case where you would want to do that is inside a piece of code that gets executed delayed (eg. a function) which imports a module, and building the isolated environment and restoring to the previous state has some performance impacts.
Also note that the context will only remove packages on exit that have actually been imported from the list of paths specified in the path argument, but not modules from the standard library, for example.
Parameters
- path – A list of paths that are added to ^{tt9}$ inside the context manager. Can also be a single string. If one or more relative paths are passed, they are treated relative to the parent_dir argument.
- parent_dir – A path that is concatenated with relative paths passed to the path argument. If this argument is omitted or ^{tt10}$, it will default to the parent directory of the file that called the ^{tt11}$ constructor (using ^{tt12}$).
- do_eggs – A boolean that indicates whether ^{tt13}$ files or directories found in the additional paths are added to ^{tt9}$.
- do_pth – A boolean that indicates whether ^{tt15}$ files found in the additional paths will be evaluated.
- do_autodisable – A boolean that indicates that ^{tt16}$ should be called automatically be the context manager.
Changed in 1.7 Added ^{tt17}$ parameter.
localimport.autodisable()
Uses ^{tt19}$ to automatically detect modules that could be imported from the paths in the importer context and calls #disable on all of them.
New in 1.7
localimport.disable(modules)
Disable one or more modules by moving them from the global module cache (^{tt21}$) to a dictionary of temporary hidden modules in the isolated environment. Once the ^{tt11}$ context manager exits, these modules will be restored. Does nothing when a module does not exist.
Parameters
- modules – A list of module names or a single module name string.
localimport.discover()
A shorthand for ^{tt24}$.
New in 1.7
版权所有©2018 Niklas Rosenstein