zipimport — Import modules from Zip archives¶
Source code: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.
Changed in version 3.8:Previously, ZIP archives with an archive comment were not supported.
See also
- 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.
- PEP 302 - New Import Hooks
The PEP to add the import hooks that help this module work.
This module defines an exception:
- exception
zipimport.ZipImportError¶ Exception raised by zipimporter objects. It’s a subclass of
ImportError,so it can be caught asImportError, too.
zipimporter Objects¶
zipimporter is the class for importing ZIP files.
- class
zipimport.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, anarchivepathof
foo/bar.zip/libwill look for modules in thelibdirectoryinside the ZIP filefoo/bar.zip(provided that it exists).ZipImportErroris raised ifarchivepath doesn’t point to a valid ZIParchive.find_module(fullname[,path])¶Search for a module specified byfullname.fullname must be the fullyqualified (dotted) module name. It returns the zipimporter instance itselfif the module was found, or
Noneif it wasn’t. The optionalpath argument is ignored—it’s there for compatibility with theimporter protocol.
get_code(fullname)¶Return the code object for the specified module. Raise
ZipImportErrorif the module couldn’t be found.
get_filename(fullname)¶Return the value
__file__would be set to if the specified modulewas imported. RaiseZipImportErrorif the module couldn’t befound.New in version 3.1.
get_source(fullname)¶Return the source code for the specified module. Raise
ZipImportErrorif the module couldn’t be found, returnNoneif the archive does contain the module, but has no sourcefor it.
is_package(fullname)¶Return
Trueif the module specified byfullname is a package. RaiseZipImportErrorif the module couldn’t be found.
load_module(fullname)¶Load the module specified byfullname.fullname must be the fullyqualified (dotted) module name. It returns the imported module, or raises
ZipImportErrorif it wasn’t found.
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.
The
archiveandprefixattributes, when combined with aslash, equal the originalarchivepath argument given to thezipimporterconstructor.
Examples¶
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') # Add .zip file to front of path>>> import jwzthreading>>> jwzthreading.__file__'example.zip/jwzthreading.py'