Skip to main content

lazyimports

A backport of Python 3.15's lazy import statement for older Python versions.

On Python 3.15+, you can write:

lazy import numpy
lazy from os.path import join

This package provides the same deferred-import behaviour on every supported Python version (3.6 through 3.15) via a small, focused API.

Why?

Deferring heavy imports until they are actually needed can noticeably speed up module load times and reduce memory usage — useful for CLIs, plugins, and any code path that imports optional dependencies.

Installation

pip install python-lazyimports

Usage

Lazy import

from lazyimports import lazy_import

np = lazy_import("numpy")          # not imported yet
pd = lazy_import("pandas")         # not imported yet

# ``numpy`` is loaded on first attribute access:
arr = np.array([1, 2, 3])

Lazy from X import Y

from lazyimports import lazy_from

join, basename = lazy_from("os.path", "join", "basename")
result = join("a", "b")            # os.path is imported here

Grouping with lazy()

from lazyimports import lazy, lazy_import

with lazy():
    np = lazy_import("numpy")
    pd = lazy_import("pandas")
    tf = lazy_import("tensorflow")
# None of the modules are imported until first use.

Inspecting and forcing load

from lazyimports import is_lazy, force_load

proxy = lazy_import("json")
assert is_lazy(proxy)
assert not is_lazy(json)           # False for already-imported modules

real = force_load(proxy)           # resolve now and return the real module

API

See the main documentation

Python 3.15+ native syntax

When you are running on Python 3.15+, you may prefer the native syntax. The package's own API still works identically, so you can use either:

# Native (Python 3.15+ only):
lazy import numpy

# Cross-version equivalent via this package:
from lazyimports import lazy_import
numpy = lazy_import("numpy")

Compatibility

  • Python 3.6 through 3.15
  • No third-party dependencies — uses only the standard library (contextlib, importlib, sys, types)

Running the tests

python -m unittest test_lazyimports -v

Retired

This project will reach its end-of-life around October 1, 2031 — the official EOL date of Python 3.15 — and the exact timeline could be slightly delayed. We plan to ship the final stable release in November 2031. After this release, all support will cease and the repository will be officially archived, as this library is developed solely to bring lazy import compatibility to Python 3.15 and older versions.

License

MIT

Metadata

Release files for python-lazyimports 0.1.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-lazyimports 0.1.5
File Size Uploaded Provenance
python_lazyimports-0.1.5.tar.gz 37.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for python-lazyimports 0.1.5
File Interpreter ABI Platform
python_lazyimports-0.1.5-py3-none-any.whl Python 3 none any Details

Total release size: 60.8 kB

Release files / python_lazyimports-0.1.5.tar.gz

Download URL python_lazyimports-0.1.5.tar.gz
Size 37.2 kB
Tags Source
SHA-256 checksum
How to use checksums
48487463b8d787f58fd7e097eb61c13a057014a53f5a08644c4364b791d46564
BLAKE2b-256 checksum
How to use checksums
9ae609d14492ed6112e8638c4f17edd5b4f2c0b79ea2935c50ee8c86b1c3b7bc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.6

Release files / python_lazyimports-0.1.5-py3-none-any.whl

Download URL python_lazyimports-0.1.5-py3-none-any.whl
Size 23.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
351bf784e33f06b28a4ff359ad637bca10bc9b19c73cf6b8ae7bc2bfb7a42dce
BLAKE2b-256 checksum
How to use checksums
9f517aa31aaa5f798a95730643b98409304427b5bae5e515fb337d8970f693ff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.6

Security for python-lazyimports 0.1.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page