zipimport --- 从 Zip 存档中导入模块

源代码: Lib/zipimport.py


此模块添加了从 ZIP 格式档案中导入 Python 模块( *.py*.pyc )和包的能力。通常不需要明确地使用 zipimport 模块,内置的 import 机制会自动将此模块用于 ZIP 档案路径的 sys.path 项目上。

通常, sys.path 是字符串的目录名称列表。此模块同样允许 sys.path 的一项成为命名 ZIP 文件档案的字符串。 ZIP 档案可以容纳子目录结构去支持包的导入,并且可以将归档文件中的路径指定为仅从子目录导入。比如说,路径 example.zip/lib/ 将只会从档案中的 lib/ 子目录导入。

任何文件都可以存在于 ZIP档案之中,但是只有 .py.pyc 文件是能够导入的。不允许导入 ZIP 中的动态模组( .pyd.so )。请注意,如果档案中只包含 .py 文件, Python不会尝试通过添加对应的 .pyc 文件修改档案,意思是如果 ZIP 档案不包含 .pyc 文件,导入或许会变慢。

在 3.8 版更改: 以前,不支持带有档案注释的 ZIP 档案。

参见

PKZIP Application Note

Phil Katz 编写的 ZIP 文件格式文档,此格式和使用的算法的创建者。

PEP 273 -  从ZIP压缩包导入模块

由 James C. Ahlstrom 编写,他也提供了实现。 Python 2.3 遵循 PEP 273 的规范,但是使用 Just van Rossum 编写的使用了 PEP 302 中描述的导入钩的实现。

PEP 302 - 新导入钩

PEP 添加导入钩来有助于模块运作。

此模块定义了一个异常:

exception zipimport.ZipImportError

异常由 zipimporter 对象引发。这是 ImportError 的子类,因此,也可以捕获为 ImportError

zipimporter 对象

zipimporter 是用于导入 ZIP 文件的类。

class zipimport.zipimporter(archivepath)

创建新的 zipimporter 实例。 archivepath 必须是指向 ZIP 文件的路径,或者 ZIP 文件中的特定路径。例如, foo/bar.zip/libarchivepath 将在 ZIP 文件 foo/bar.zip 中的 lib 目录中查找模块(只要它存在)。

如果 archivepath 没有指向一个有效的 ZIP 档案,引发 ZipImportError

find_module(fullname[, path])

Search for a module specified by fullname. fullname must be the fully qualified (dotted) module name. It returns the zipimporter instance itself if the module was found, or None if it wasn't. The optional path argument is ignored---it's there for compatibility with the importer protocol.

get_code(fullname)

Return the code object for the specified module. Raise ZipImportError if the module couldn't be found.

get_data(pathname)

Return the data associated with pathname. Raise OSError if the file wasn't found.

在 3.3 版更改: IOError used to be raised instead of OSError.

get_filename(fullname)

Return the value __file__ would be set to if the specified module was imported. Raise ZipImportError if the module couldn't be found.

3.1 新版功能.

get_source(fullname)

Return the source code for the specified module. Raise ZipImportError if the module couldn't be found, return None if the archive does contain the module, but has no source for it.

is_package(fullname)

Return True if the module specified by fullname is a package. Raise ZipImportError if the module couldn't be found.

load_module(fullname)

Load the module specified by fullname. fullname must be the fully qualified (dotted) module name. It returns the imported module, or raises ZipImportError if it wasn't found.

archive

The file name of the importer's associated ZIP file, without a possible subpath.

prefix

The subpath within the ZIP file where modules are searched. This is the empty string for zipimporter objects which point to the root of the ZIP file.

The archive and prefix attributes, when combined with a slash, equal the original archivepath argument given to the zipimporter constructor.

示例

Here is an example that imports a module from a ZIP archive - note that the zipimport module is not explicitly used.

$ 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'