zipimport
导入模块
源代码: Lib/zipimport.py
This module adds the ability to import Python modules ( *.py , *.pyc ) and packages from ZIP-format archives. It is usually not needed to use the zipimport module explicitly; it is automatically used by the built-in import mechanism for sys.path items that are paths to ZIP archives.
*.py
*.pyc
import
sys.path
Typically, sys.path is a list of directory names as strings. This module also 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 a subdirectory. For example, the path example.zip/lib/ would only import from the lib/ subdirectory within the archive.
example.zip/lib/
lib/
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 the corresponding .pyc file, meaning that if a ZIP archive doesn’t contain .pyc files, importing may be rather slow.
.py
.pyc
.pyd
.so
3.8 版改变: Previously, ZIP archives with an archive comment were not supported.
另请参阅
Documentation on the ZIP file format by Phil Katz, the creator of the format and algorithms used.
Written by James C. Ahlstrom, who also provided an implementation. Python 2.3 follows the specification in PEP 273 , but uses an implementation written by Just van Rossum that uses the import hooks described in PEP 302 .
importlib
Package providing the relevant protocols for all importers to implement.
This module defines an exception:
Exception raised by zipimporter objects. It’s a subclass of ImportError , so it can be caught as ImportError , too.
ImportError
zipimporter is the class for importing ZIP files.
zipimporter
Create a new zipimporter instance. archivepath must be a path to a ZIP file, 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 directory inside the ZIP file foo/bar.zip (provided that it exists).
foo/bar.zip/lib
lib
foo/bar.zip
ZipImportError 被引发若 archivepath doesn’t point to a valid ZIP archive.
ZipImportError
Changed in version 3.12: 方法 find_loader() and find_module() , deprecated in 3.10 are now removed. Use find_spec() 代替。
find_loader()
find_module()
find_spec()
Implementation of importlib.abc.Loader.create_module() that returns None to explicitly request the default semantics.
importlib.abc.Loader.create_module()
None
Added in version 3.10.
Implementation of importlib.abc.Loader.exec_module() .
importlib.abc.Loader.exec_module()
An implementation of importlib.abc.PathEntryFinder.find_spec() .
importlib.abc.PathEntryFinder.find_spec()
Return the code object for the specified module. Raise ZipImportError if the module couldn’t be imported.
Return the data associated with pathname 。引发 OSError if the file wasn’t found.
OSError
3.3 版改变: IOError 用于被引发,它现在是别名化的 OSError .
IOError
Return the value __file__ would be set to if the specified module was imported. Raise ZipImportError if the module couldn’t be imported.
__file__
Added in version 3.1.
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.
返回 True if the module specified by fullname is a package. Raise ZipImportError if the module couldn’t be found.
True
Load the module specified by fullname . fullname must be the fully qualified (dotted) module name. Returns the imported module on success, raises ZipImportError 当故障时。
从 3.10 版起弃用: 使用 exec_module() 代替。
exec_module()
Clear out the internal cache of information about files found within the ZIP archive.
The file name of the importer’s associated ZIP file, without a possible subpath.
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 构造函数。
archive
prefix
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'
pkgutil — 包扩展实用程序
pkgutil
键入搜索术语或模块、类、函数名称。