This module adds the ability to import Python modules (*.py,*.py[co]) 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 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 thecorresponding.pyc or.pyo file, meaning that if a ZIP archivedoesn’t contain.pyc files, importing may be rather slow.
ZIP archives with an archive comment are currently not supported.
See also
This module defines an exception:
Exception raised by zipimporter objects. It’s a subclass ofImportError,so it can be caught asImportError, too.
zipimporter is the class for importing ZIP files.
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.
Search for a module specified byfullname.fullname must be the fullyqualified (dotted) module name. It returns the zipimporter instance itselfif the module was found, orNone if it wasn’t. The optionalpath argument is ignored—it’s there for compatibility with theimporter protocol.
Return the code object for the specified module. RaiseZipImportError if the module couldn’t be found.
Return the value__file__ would be set to if the specified modulewas imported. RaiseZipImportError if the module couldn’t befound.
New in version 3.1.
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.
ReturnTrue if the module specified byfullname is a package. RaiseZipImportError if the module couldn’t be found.
Load the module specified byfullname.fullname must be the fullyqualified (dotted) module name. It returns the imported module, or raisesZipImportError if it wasn’t found.
The file name of the importer’s associated ZIP file, without a possiblesubpath.
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 -l example.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'
30.1.imp — Access theimport internals
30.3.pkgutil — Package extension utility
Enter search terms or a module, class or function name.