Skip to content
AITroveRead. Build. Understand.
Make this comfortable

Python module cache: repeated imports reuse an owned module object

Last updated: 30 Sept 20264 min read
tutorial
BeginnerBy AITrove Editorial

Python caches imported modules by name in sys.modules so a later import of the same resolved name usually returns the existing module object.

Download Python source kit

Operation contract

The fixture creates a temporary owned module, imports it twice and observes one object and one initialization. It removes only its own cache entry before importing again, which creates a second module with fresh state. The cleanup also removes the temporary directory from sys.path. This is an inspection of import lifetime, not a recommendation to delete shared process modules during application requests.

Failure and ownership boundary

A module name is not a proof of source provenance. Modifying sys.path, shadowing a package name or importing untrusted code can run unexpected initialization. Tests should isolate interpreter state; services should use explicit package layouts and trusted installation. Python modules: separate import-time definitions from program execution, Python import failures: module removal does not roll back every side effect and Python package layout: owned imports and module entrypoints address the broader boundary.

Working program

python
import importlib
import sys
import tempfile
from pathlib import Path

with tempfile.TemporaryDirectory() as directory:
    module_file = Path(directory) / "aitrove_owned_counter.py"
    module_file.write_text("starts = 1\n", encoding="utf-8")
    sys.path.insert(0, directory)
    importlib.invalidate_caches()
    try:
        first = importlib.import_module("aitrove_owned_counter")
        first.starts += 1
        second = importlib.import_module("aitrove_owned_counter")
        print("same object:", first is second, second.starts)
        sys.modules.pop("aitrove_owned_counter", None)
        third = importlib.import_module("aitrove_owned_counter")
        print("fresh state:", third is first, third.starts)
    finally:
        sys.modules.pop("aitrove_owned_counter", None)
        sys.path.remove(directory)

Output

Output
same object: True 2
fresh state: False 1

Costs and limits

Module initialization runs when its name is first loaded in this interpreter. Reimport lookup is usually dictionary-backed, but finder/path behavior and arbitrary module initialization prevent a universal constant-time import claim. The temporary filesystem fixture tests one process only.

Common Mistakes

  • Do not delete a shared module from sys.modules in a live service.
  • A matching name does not establish where the imported bytes came from.

Connected lessons

Python modules: separate import-time definitions from program execution, Python import failures: module removal does not roll back every side effect, Python package layout: owned imports and module entrypoints.

python
import-cache-ownership
Storage details