python-lazy-importer
Port variant v15
Summary Context manager for opt-in lazy imports (3.15)
Package version 0.1.0
Homepage No known homepage
Keywords python
Maintainer Python Automaton
License Not yet specified
Other variants v14
Ravenports Buildsheet | History
Ravensource Port Directory | History
Last modified 13 NOV 2026, 13:25:52 UTC
Port created 11 NOV 2026, 00:33:11 UTC
Subpackage Descriptions
single # lazy-importer A backwards-compatibility shim allowing libraries to get lazy import semantics similar to [PEP 810] automatically in Python 3.11+ without needing to set your minimum version to 3.15. In Python 3.15+, this falls back to the standard lazy imports mechanism. ## Usage Install from a checkout with `python -m pip install .`: ```python from lazy_importer import lazy_importer __lazy_modules__ = {"json", "pathlib"} with lazy_importer(): import json from pathlib import Path # Importing this module doesn't load json or pathlib for these statements. def read_config(filename: str) -> object: return json.loads(Path(filename).read_text(encoding="utf-8")) ``` `__lazy_modules__` determines which imports are lazy. On Python 3.11–3.14, the context enables the fallback, which checks the importing module's declaration at each import. On Python 3.15+, Python reads the declaration directly and the context is a no-op, leaving native controls and filtering in effect without loading the fallback implementation. Without a declaration, imports remain eager under the default interpreter settings. `__lazy_modules__` contains the names of the modules whose imports should be lazy. For a `from` import, the selected module is the one after `from`: | Import statement | Entry in `__lazy_modules__` | | --- | --- | | `import example.child` | `"example.child"` | | `from example import child` | `"example"` | | `from example.child import value` | `"example.child"` | | `from .child import value` (inside `example`) | `"example.child"` | | `from . import child` (inside `example`) | `"example"` | With a set of names, selecting `"example"` doesn't also select imports of `"example.child"`. This is also how PEP 810's `__lazy_modules__` works. Both paths check membership in `__lazy_modules__` at each import, so changes to the declaration take effect within an active context. A custom container can select names through `__contains__` without supporting iteration. An empty or absent declaration selects no imports. Each call to `lazy_importer()` creates a context. The fallback context can only be entered once, so separate `with` blocks need separate calls even if they use the same selection. This is why the example calls `lazy_importer` in the `with` statement rather than storing the context for reuse. ## When imports run In the example above, importing your module does not load `json` or `pathlib` on behalf of those statements. Calling `read_config()` loads them when it reaches the operations that need them. Any side effects of their initialization, such as registering plugins or configuring logging, are delayed along with the import. A module imported only for its side effects therefore needs an eager import, or an explicit call to `resolve()` to trigger initialization. Import failures are delayed too. If one of the selected modules is missing or raises an exception during initialization, that exception reaches the code that first uses it. A `try`/`except` around the import block is therefore not a portable way to check whether an optional dependency is available. After a failed import, another use can retry it. Once loading starts, the module's own imports run normally unless it opts into lazy imports itself. The package uses the import hooks and search path in effect at first use, so changes to the import environment between declaration and use can affect what gets loaded. With `from module import first, second`, loading `first` does not resolve the binding for `second`; each imported name is resolved when it is needed. ## Differences from PEP 810
Configuration Switches (platform-specific settings discarded)
PY314 OFF Build using Python 3.14 PY315 ON Build using Python 3.15
Package Dependencies by Type
Build (only) python315:dev:std
python-pip:single:v15
autoselect-python:single:std
Build and Runtime python315:primary:std
Download groups
main mirror://PYPIWHL/0a/28/7ded071d0975dcc6d5d106ffbf7b3ca420f7caa3b8a235fa604b6e37032a
Distribution File Information
bd93fab7bef18e842eea02b839410954d37889140dc64ca4d9680aec6880c739 12580 python-src/lazy_importer-0.1.0-py3-none-any.whl
Ports that require python-lazy-importer:v15
No other ports depend on this one.