Import and Packaging Modules

zipimport

Finds Python modules and packages stored inside ZIP archives. Python source and bytecode can be loaded from supported archive entries, but native extension modules cannot be imported this way. The example retrieves a code object from a trusted temporary archive without executing it.

Formal Syntax & Structure

python syntaxpython
import zipimport

Return Value

module

Module namespace available through import; import is a statement, not a value-returning expression.

Working Example

Example Snippetpython
from zipimport import zipimporterfrom zipfile import ZipFilefrom pathlib import Pathfrom tempfile import TemporaryDirectorywith TemporaryDirectory() as d:    p = Path(d) / "demo.zip"    with ZipFile(p, "w") as z:        z.writestr("demo.py", "value = 7\n")    loader = zipimporter(str(p))    print(loader.get_code("demo").co_name)
Python Reference