• zipimport —- Import modules from Zip archives
    • zipimporter Objects
    • 示例

    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 the zipimport module explicitly; it is automatically usedby the built-in import mechanism for sys.path items that are pathsto ZIP archives.

    Typically, sys.path is a list of directory names as strings. This modulealso allows an item of sys.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 path example.zip/lib/ would onlyimport from the lib/ subdirectory within the archive.

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

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

    参见

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

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

    • Written by James C. Ahlstrom, who also provided an implementation. Python 2.3follows the specification in PEP 273, but uses an implementation written by Justvan Rossum that uses the import hooks described in PEP 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 as ImportError, 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, an _archivepath_of foo/bar.zip/lib will look for modules in the lib directoryinside the ZIP file foo/bar.zip (provided that it exists).

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

    • findmodule(_fullname[, path])
    • Search for a module specified by fullname. fullname must be the fullyqualified (dotted) module name. It returns the zipimporter instance itselfif the module was found, or None if it wasn't. The optionalpath argument is ignored—-it's there for compatibility with theimporter protocol.

    • getcode(_fullname)

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

    • getdata(_pathname)

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

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

    • getfilename(_fullname)
    • Return the value file would be set to if the specified modulewas imported. Raise ZipImportError if the module couldn't befound.

    3.1 新版功能.

    • getsource(_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.

    • ispackage(_fullname)

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

    • loadmodule(_fullname)

    • Load the module specified by fullname. fullname must be the fullyqualified (dotted) module name. It returns the imported module, or raisesZipImportError if 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 archive and prefix attributes, when combined with aslash, equal the original archivepath 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.

    1. $ unzip -l example.zip
    2. Archive: example.zip
    3. Length Date Time Name
    4. -------- ---- ---- ----
    5. 8467 11-26-02 22:30 jwzthreading.py
    6. -------- -------
    7. 8467 1 file
    8. $ ./python
    9. Python 2.3 (#1, Aug 1 2003, 19:54:32)
    10. >>> import sys
    11. >>> sys.path.insert(0, 'example.zip') # Add .zip file to front of path
    12. >>> import jwzthreading
    13. >>> jwzthreading.__file__
    14. 'example.zip/jwzthreading.py'