On this page
zipimport.zipimporter
Up: zipimport | Next: zipimport.ZipImportError
Rel: zipimport.ZipImportError
Ref: docs.python.org — zipimport.zipimporter
Tags: #public #python3 #stdlib #zipimport
Import
import zipimport
from zipimport import zipimporter
Signature & Overview
zipimport.zipimporter(path)
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
Methods & Attributes
zipimporter.create_module(self, spec)— Use default semantics for module creation.zipimporter.exec_module(self, module)— Execute the module.zipimporter.find_spec(self, fullname, target=None)— Create a ModuleSpec for the specified module.zipimporter.get_code(self, fullname)— Return the code object for the specified module. Raise ZipImportError if the module couldn't be imported.zipimporter.get_data(self, pathname)— Return the data associated with 'pathname'. Raise OSError if the file wasn't found.zipimporter.get_filename(self, fullname)— Return the filename for the specified module or raise ZipImportError if it couldn't be imported.zipimporter.get_resource_reader(self, fullname)— Return the ResourceReader for a module in a zip file.zipimporter.get_source(self, fullname)— 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.zipimporter.invalidate_caches(self)— Invalidates the cache of file data of the archive path.zipimporter.is_package(self, fullname)— Return True if the module specified by fullname is a package.zipimporter.load_module(self, fullname)— Load the module specified by 'fullname'. 'fullname' must be the fully qualified (dotted) module name. It returns the imported module, or raises ZipImportError if it could not be imported.
Usage
from pathlib import Path
import tempfile, zipfile, zipimport
with tempfile.TemporaryDirectory() as tmp:
z = Path(tmp) / 'pkg.zip'
with zipfile.ZipFile(z, 'w') as zf:
zf.writestr('hi.py', 'VAL = 99\n')
imp = zipimport.zipimporter(str(z))
assert imp.get_source('hi').strip() == 'VAL = 99'
Gotchas
- Pure-Python modules only: Cannot import C extensions directly from zip files.
REPL Inspect
from zipimport import zipimporter
help(zipimporter)
Linked from
Related notes
- tuts/python3/stdlib_module/zipimport/ZipImportError Linked here; links to this page; shared tags: #stdlib, #zipimport
- tuts/python3/stdlib_module/zipimport Linked here; links to this page; shared tags: #stdlib
- tuts/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib
- tuts/c/stdlib/assert/assert shared tags: #stdlib
- tuts/c/stdlib/assert/static_assert shared tags: #stdlib
- tuts/c/stdlib/ctype shared tags: #stdlib
- tuts/c/stdlib/ctype/isalpha shared tags: #stdlib
- tuts/c/stdlib/ctype/isdigit shared tags: #stdlib
- tuts/c/stdlib/ctype/isspace shared tags: #stdlib