Skip to main content

Backport of Python 3.15's lazy import statement.

Project description

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

Symbol Description
lazy_import(name, package=None) Return a lazy proxy for module name.
lazy_from(module, *names) Lazily import specific names from a module.
is_lazy(obj) Return True if obj is a LazyModule.
force_load(obj) Force resolution of a LazyModule; pass-through for anything else.
lazy() Context manager for grouping lazy imports (no-op marker).
LazyModule The proxy class returned by lazy_import().
NATIVE_LAZY_IMPORT True when running on Python 3.15+.
SUPPORT_LAZY_IMPORT True on every version (this package provides its own implementation).

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

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_lazyimports-0.1.1.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_lazyimports-0.1.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file python_lazyimports-0.1.1.tar.gz.

File metadata

  • Download URL: python_lazyimports-0.1.1.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.6

File hashes

Hashes for python_lazyimports-0.1.1.tar.gz
Algorithm Hash digest
SHA256 1a353452ce6107150e455b5581c293adb0af0d915f906dacb7e52a7112a3c858
MD5 503b9500f912645cb0e53c98cd554b6d
BLAKE2b-256 7743a08bc55ee285944902bc030f979b6e4f758338dad53a6495124dba34d381

See more details on using hashes here.

File details

Details for the file python_lazyimports-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for python_lazyimports-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fef90919bf9f5b9e0404517357335e22dcca4d1a448a957e9fd06abdeeced233
MD5 7b7b51303a9bbf94c98a15f841289f38
BLAKE2b-256 0dc14588439a715d6abe8ee37bfad58ba53b2c5ecbbebf4f7a0cc50d0ec370a9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page