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作为 python模块当您真正想导入python模块时 localimport模块本身的导入不会被隔离。
解决方案是直接在 你的申请代码。通常您会使用缩小版。

可以在下面找到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

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

推荐PyPI第三方库


热门话题
java Clojure关键字在内存中的大小是多少?   Java中有固定长度的通用数组对象吗?   PostgreSQL:通过Java更新我的用户表   错误:使用java解析xml   java Json显示列表中对象的名称   java比较JodaTime时区   与JAVA中的API和包的区别?   java的int值在for循环中不改变   谷歌应用引擎中的java RSA   迁移到spring 5后出现java非法字符错误   java Websphere管理控制台不工作   JavaGSON如何始终在json中包含毫秒?   带有空格和双引号的windows Java ProcessBuilder命令参数失败   java错误:重复的zip条目[43.jar:org/apache/http/annotation/NotThreadSafe.class]