| 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
|