zipimport --- 從 Zip 封存檔案匯入模組

原始碼:Lib/zipimport.py


This module adds the ability to import Python modules (*.py,*.pyc) and packages from ZIP-format archives. It is usually notneeded to use thezipimport module explicitly; it is automatically usedby the built-inimport mechanism forsys.path items that are pathsto ZIP archives.

Typically,sys.path is a list of directory names as strings. This modulealso allows an item ofsys.path to be a string naming a ZIP file archive.The ZIP archive can contain a subdirectory structure to support package imports,and a path within the archive can be specified to only import from asubdirectory. For example, the pathexample.zip/lib/ would onlyimport from thelib/ subdirectory within the archive.

Any files may be present in the ZIP archive, but importers are only invoked for.py and.pyc files. 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 thecorresponding.pyc file, meaning that if a ZIP archivedoesn't contain.pyc files, importing may be rather slow.

在 3.13 版的變更:ZIP64 is supported

在 3.8 版的變更:Previously, ZIP archives with an archive comment were not supported.

也參考

PKZIP Application Note

Documentation on the ZIP file format by Phil Katz, the creator of the format andalgorithms used.

PEP 273 - Import Modules from Zip Archives

Written by James C. Ahlstrom, who also provided an implementation. Python 2.3follows the specification inPEP 273, but uses an implementation written by Justvan Rossum that uses the import hooks described inPEP 302.

importlib - The implementation of the import machinery

Package providing the relevant protocols for all importers toimplement.

This module defines an exception:

exceptionzipimport.ZipImportError

Exception raised by zipimporter objects. It's a subclass ofImportError,so it can be caught asImportError, too.

zipimporter 物件

zipimporter is the class for importing ZIP files.

classzipimport.zipimporter(archivepath)

Create a new zipimporter instance.archivepath must be a path to a ZIPfile, or to a specific path within a ZIP file. For example, anarchivepathoffoo/bar.zip/lib will look for modules in thelib directoryinside the ZIP filefoo/bar.zip (provided that it exists).

ZipImportError is raised ifarchivepath doesn't point to a valid ZIParchive.

在 3.12 版的變更:Methodsfind_loader() andfind_module(), deprecated in 3.10 arenow removed. Usefind_spec() instead.

create_module(spec)

Implementation ofimportlib.abc.Loader.create_module() that returnsNone to explicitly request the default semantics.

在 3.10 版被加入.

exec_module(module)

Implementation ofimportlib.abc.Loader.exec_module().

在 3.10 版被加入.

find_spec(fullname,target=None)

An implementation ofimportlib.abc.PathEntryFinder.find_spec().

在 3.10 版被加入.

get_code(fullname)

Return the code object for the specified module. RaiseZipImportError if the module couldn't be imported.

get_data(pathname)

Return the data associated withpathname. RaiseOSError if thefile wasn't found.

在 3.3 版的變更:IOError used to be raised, it is now an alias ofOSError.

get_filename(fullname)

Return the value__file__ would be set to if the specified modulewas imported. RaiseZipImportError if the module couldn't beimported.

在 3.1 版被加入.

get_source(fullname)

Return the source code for the specified module. RaiseZipImportError if the module couldn't be found, returnNone if the archive does contain the module, but has no sourcefor it.

is_package(fullname)

ReturnTrue if the module specified byfullname is a package. RaiseZipImportError if the module couldn't be found.

load_module(fullname)

Load the module specified byfullname.fullname must be the fullyqualified (dotted) module name. Returns the imported module on success,raisesZipImportError on failure.

在 3.10 版之後被棄用:Useexec_module() instead.

invalidate_caches()

Clear out the internal cache of information about files found withinthe ZIP archive.

在 3.10 版被加入.

archive

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

prefix

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

Thearchive andprefix attributes, when combined with aslash, equal the originalarchivepath argument given to thezipimporter constructor.

範例

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

$unzip-lexample.zipArchive:  example.zip  Length     Date   Time    Name --------    ----   ----    ----     8467  11-26-02 22:30   jwzthreading.py --------                   -------     8467                   1 file$./pythonPython 2.3 (#1, Aug 1 2003, 19:54:32)>>> import sys>>> sys.path.insert(0, 'example.zip')  # 將 .zip 檔案新增到系統路徑的最前面>>> import jwzthreading>>> jwzthreading.__file__'example.zip/jwzthreading.py'