31.4. zipimport — 从 Zip 存档中导入模块¶
2.3 新版功能.
This module adds the ability to import Python modules (*.py,
*.py[co]) and packages from ZIP-format archives. It is usually not
needed to use the zipimport module explicitly; it is automatically used
by the built-in import mechanism for sys.path items that are paths
to ZIP archives.
通常, sys.path 是字符串的目录名称列表。此模块同样允许 sys.path 的一项成为命名 ZIP 文件档案的字符串。 ZIP 档案可以容纳子目录结构去支持包的导入,并且可以将归档文件中的路径指定为仅从子目录导入。比如说,路径 example.zip/lib/ 将只会从档案中的 lib/ 子目录导入。
Any files may be present in the ZIP archive, but only files .py and
.py[co] are available for import. ZIP import of dynamic modules
(.pyd, .so) is disallowed. Note that if an archive only contains
.py files, Python will not attempt to modify the archive by adding the
corresponding .pyc or .pyo file, meaning that if a ZIP archive
doesn’t contain .pyc files, importing may be rather slow.
Using the built-in reload() function will fail if called on a module
loaded from a ZIP archive; it is unlikely that reload() would be needed,
since this would imply that the ZIP has been altered during runtime.
目前不支持带有档案注释的 ZIP 归档。
参见
- PKZIP Application Note
Phil Katz 编写的 ZIP 文件格式文档,此格式和使用的算法的创建者。
- PEP 273 - 从ZIP压缩包导入模块
由 James C. Ahlstrom 编写,他还提供了一个具体实现。 Python 2.3 遵循 PEP 273 的规范,但使用了一个由 van Rossum 本人所编写的实现,该实现使用了 PEP 302 所描述的导入钩子。
- PEP 302 - 新导入钩
PEP 添加导入钩来有助于模块运作。
此模块定义了一个异常:
-
exception
zipimport.ZipImportError¶ 异常由 zipimporter 对象引发。这是
ImportError的子类,因此,也可以捕获为ImportError。
31.4.1. zipimporter 对象¶
zipimporter 是用于导入 ZIP 文件的类。
-
class
zipimport.zipimporter(archivepath)¶ 创建新的 zipimporter 实例。 archivepath 必须是指向 ZIP 文件的路径,或者 ZIP 文件中的特定路径。例如,
foo/bar.zip/lib的 archivepath 将在 ZIP 文件foo/bar.zip中的lib目录中查找模块。如果 archivepath 没有指向一个有效的 ZIP 档案,引发
ZipImportError。-
find_module(fullname[, path])¶ 搜索由 fullname 指定的模块。 fullname 必须是完全合格的(含加点作分割的拓展名)模块名。它返回 zipimporter 实例本身如果模块被找到,或者返回
None如果没找到指定模块。可选的 path 被忽略,这是为了与导入器协议兼容。
-
get_code(fullname)¶ 返回指定模块的代码对象。如果不能找到模块会引发
ZipImportError错误。
-
get_data(pathname)¶ Return the data associated with pathname. Raise
IOErrorif the file wasn’t found.
-
get_filename(fullname)¶ 如果导入了指定的模块
__file__,则返回为该模块设置的值。如果未找到模块则引发ZipImportError错误。
2.7 新版功能.
-
get_source(fullname)¶ 返回指定模块的源代码。如果没有找到模块则引发
ZipImportError,如果档案包含模块但是没有源代码,返回None。
-
is_package(fullname)¶ 如果由 fullname 指定的模块是一个包则返回
True。如果不能找到模块则引发ZipImportError错误。
-
load_module(fullname)¶ 加载由 fullname 指定的模块。 fullname 必须是完全合格的(含加点作为分割的拓展名)模块名。它返回已加载模块,或者当找不到模块时引发
ZipImportError错误。
-
archive¶ 导入器关联的 ZIP 文件的文件名,不含可能的子路径。
-
prefix¶ ZIP 文件中搜索的模块的子路径。这是一个指向 ZIP 文件 根目录的 zipimporter 对象的空字符串。
当与斜杠结合使用时,
archive和prefix属性等价于赋予zipimporter构造器的原始 archivepath 参数。-
31.4.2. 例子¶
这是一个从 ZIP 档案中导入模块的例子 - 请注意 zipimport 不需要明确地使用。
$ unzip -l example.zip
Archive: example.zip
Length Date Time Name
-------- ---- ---- ----
8467 11-26-02 22:30 jwzthreading.py
-------- -------
8467 1 file
$ ./python
Python 2.3 (#1, Aug 1 2003, 19:54:32)
>>> import sys
>>> sys.path.insert(0, 'example.zip') # Add .zip file to front of path
>>> import jwzthreading
>>> jwzthreading.__file__
'example.zip/jwzthreading.py'
