zipimport - zipimport provides support for importing Python modules from Zip archives.
| Use Case | Command | Description |
|---|---|---|
| Create importer | zipimporter("/path/to/archive.zip") | Make a zipimporter for a Zip file or internal path |
| Find module spec | importer.find_spec("mymodule") | Get a ModuleSpec for the module (modern API) |
| Get code object | importer.get_code("mymodule") | Return compiled code object for the module |
| Get source code | importer.get_source("mymodule") | Return source string; None if no source in archive |
| Load module | importer.load_module("mymodule") | Load and return module (deprecated, use exec_module) |
| Check if package | importer.is_package("mypackage") | Return True if it's a package |
| Get file data | importer.get_data("path/inside.zip") | Return raw bytes of a file inside the archive |
| Invalidate cache | importer.invalidate_caches() | Reload zip directory info (e.g. after archive changes) |
| Resource reader | importer.get_resource_reader("mypackage") | Get a ResourceReader for package inside a zip |
| Handle errors | try: ... except ZipImportError: ... | ZipImportError is a subclass of ImportError |
https://docs.python.org/3.10/library/zipimport.html
The following documentation is automatically generated from the Python source files. It may be incomplete, incorrect or include features that are considered implementation detail and may vary between Python implementations. When in doubt, consult the module reference at the location listed above.
This module exports three objects:
zipimporter: a class; its constructor takes a path to a Zip archive.ZipImportError: exception raised by zipimporter objects. It's a subclass of ImportError, so it can be caught as ImportError too._zip_directory_cache: a dict, mapping archive paths to zip directory info dicts, as used in zipimporter._files.It is usually not needed to use the zipimport module explicitly; it is used by the builtin import mechanism for sys.path items that are paths to Zip archives.
_frozen_importlib_external._LoaderBasics(builtins.object)
zipimporter
builtins.ImportError(builtins.Exception)
ZipImportError
__weakref__: list of weak references to the objectbuiltins.ImportError:
__init__(self, /, *args, **kwargs) — Initialize self.__reduce__(...) — Helper for pickle.__str__(self, /) — Return str(self).builtins.ImportError:
msg — exception messagename — module namepath — module pathbuiltins.Exception:
__new__(*args, **kwargs) — Create and return a new object.builtins.BaseException:
__delattr__(self, name, /) — Implement delattr(self, name).__getattribute__(self, name, /) — Return getattr(self, name).__repr__(self, /) — Return repr(self).__setattr__(self, name, value, /) — Implement setattr(self, name, value).__setstate__(...)with_traceback(...) — Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.builtins.BaseException:
__cause__ — exception cause__context__ — exception context__dict____suppress_context____traceback__argszipimporter(path)
zipimporter(archivepath) -> zipimporter object
Create a new zipimporter instance. 'archivepath' must be a path to a zipfile, or to a specific path inside a zipfile. For example, it can be '/tmp/myimport.zip', or '/tmp/myimport.zip/mydirectory', if mydirectory is a valid directory inside the archive.
ZipImportError is raised if archivepath doesn't point to a valid Zip archive.
The archive attribute of zipimporter objects contains the name of the zipfile targeted.
__init__(self, path) — Initialize self.__repr__(self) — Return repr(self).find_loader(self, fullname, path=None) — find_loader(fullname, path=None) -> self, str or None. Search for a module specified by 'fullname'. 'fullname' must be the fully qualified (dotted) module name. Returns the zipimporter instance itself if the module was found, a string containing the full path name if it's possibly a portion of a namespace package, or None otherwise. The optional 'path' argument is ignored — it's there for compatibility with the importer protocol. Deprecated since Python 3.10. Use find_spec() instead.find_module(self, fullname, path=None) — find_module(fullname, path=None) -> self or None. Search for a module specified by 'fullname'. Returns the zipimporter instance itself if found, or None if not. The optional 'path' argument is ignored. Deprecated since Python 3.10. Use find_spec() instead.find_spec(self, fullname, target=None) — Create a ModuleSpec for the specified module. Returns None if the module cannot be found.get_code(self, fullname) — get_code(fullname) -> code object. Return the code object for the specified module. Raise ZipImportError if the module couldn't be imported.get_data(self, pathname) — get_data(pathname) -> string with file data. Return the data associated with 'pathname'. Raise OSError if the file wasn't found.get_filename(self, fullname) — get_filename(fullname) -> filename string. Return the filename for the specified module or raise ZipImportError if it couldn't be imported.get_resource_reader(self, fullname) — Return the ResourceReader for a package in a zip file. If 'fullname' is a package within the zip file, return the ResourceReader object for the package. Otherwise return None.get_source(self, fullname) — get_source(fullname) -> source string. 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.invalidate_caches(self) — Reload the file data of the archive path.is_package(self, fullname) — is_package(fullname) -> bool. Return True if the module specified by fullname is a package. Raise ZipImportError if the module couldn't be found.load_module(self, fullname) — load_module(fullname) -> module. Load the module specified by 'fullname'. Returns the imported module, or raises ZipImportError if it could not be imported. Deprecated since Python 3.10. Use exec_module() instead._frozen_importlib_external._LoaderBasics:
create_module(self, spec) — Use default semantics for module creation.exec_module(self, module) — Execute the module._frozen_importlib_external._LoaderBasics:
__dict__ — dictionary for instance variables__weakref__ — list of weak references to the object__all__ = ['ZipImportError', 'zipimporter']
(built-in)
Generated by phpman v4.9.27 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-07-18 18:14 @216.73.216.114
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Enhanced by LLM: deepseek-v4-flash / taotoken.net / www.chedong.com - original format