Skip to main content

PyPI Test status Test coverage

lazy-loader makes it easy to load subpackages and functions on demand.

Motivation

  1. Allow subpackages to be made visible to users without incurring import costs.
  2. Allow external libraries to be imported only when used, improving import times.

For a more detailed discussion, see the SPEC.

Installation

pip install -U lazy-loader

We recommend using lazy-loader with Python >= 3.11. If using Python 3.11, please upgrade to 3.11.9 or later. If using Python 3.12, please upgrade to 3.12.3 or later. These versions avoid a known race condition.

Usage

Lazily load subpackages

Consider the __init__.py from scikit-image:

subpackages = [
    ...,
    'filters',
    ...
]

import lazy_loader as lazy
__getattr__, __dir__, _ = lazy.attach(__name__, subpackages)

You can now do:

import skimage as ski
ski.filters.gaussian(...)

The filters subpackages will only be loaded once accessed.

Lazily load subpackages and functions

Consider skimage/filters/__init__.py:

from ..util import lazy

__getattr__, __dir__, __all__ = lazy.attach(
    __name__,
    submodules=['rank'],
    submod_attrs={
        '_gaussian': ['gaussian', 'difference_of_gaussians'],
        'edges': ['sobel', 'scharr', 'prewitt', 'roberts',
                  'laplace', 'farid']
    }
)

The above is equivalent to:

from . import rank
from ._gaussian import gaussian, difference_of_gaussians
from .edges import (sobel, scharr, prewitt, roberts,
                    laplace, farid)

Except that all subpackages (such as rank) and functions (such as sobel) are loaded upon access.

Type checkers

Static type checkers and IDEs cannot infer type information from lazily loaded imports. As a workaround you can load type stubs (.pyi files) with lazy.attach_stub:

import lazy_loader as lazy
__getattr__, __dir__, _ = lazy.attach_stub(__name__, "subpackages.pyi")

Note that, since imports are now defined in .pyi files, those are not only necessary for type checking but also at runtime.

The SPEC describes this workaround in more detail.

Early failure

With lazy loading, missing imports no longer fail upon loading the library. During development and testing, you can set the EAGER_IMPORT environment variable to "1" or "true" to disable lazy loading ("0" or "" re-enables lazy loading).

External libraries

The lazy.attach function discussed above is used to set up package internal imports.

Use lazy.load to lazily import external libraries:

sp = lazy.load('scipy')  # `sp` will only be loaded when accessed
sp.linalg.norm(...)

Note that lazily importing subpackages, i.e. load('scipy.linalg') will cause the package containing the subpackage to be imported immediately; thus, this usage is discouraged.

You can ask lazy.load to raise import errors as soon as it is called:

linalg = lazy.load('scipy.linalg', error_on_import=True)

Optional requirements

One use for lazy loading is for loading optional dependencies, with ImportErrors only arising when optional functionality is accessed. If optional functionality depends on a specific version, a version requirement can be set:

np = lazy.load("numpy", require="numpy >=1.24")

In this case, if numpy is installed, but the version is less than 1.24, the np module returned will raise an error on attribute access. Using this feature is not all-or-nothing: One module may rely on one version of numpy, while another module may not set any requirement.

Note that the requirement must use the package distribution name instead of the module import name. For example, the pyyaml distribution provides the yaml module for import.

Release files for lazy-loader 0.6

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

Source distribution (sdist)

Source distribution for lazy-loader 0.6
File Size Uploaded
lazy_loader-0.6.tar.gz 18.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for lazy-loader 0.6
File Interpreter ABI Platform
lazy_loader-0.6-py3-none-any.whl Python 3 none any Details

Total release size: 27.2 kB

Release files / lazy_loader-0.6.tar.gz

Download URL lazy_loader-0.6.tar.gz
Size 18.4 kB
Tags Source
SHA-256 checksum
How to use checksums
2f4b7824d6401958639008a0cae20c776b61dff619accd6890693e2de8260167
BLAKE2b-256 checksum
How to use checksums
198c0f2ff2a8b7513e68871a74740c17a504b0488c679b379e6445ebb7bd78dc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 21, 2026.

Transparency log

Release files / lazy_loader-0.6-py3-none-any.whl

Download URL lazy_loader-0.6-py3-none-any.whl
Size 8.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
77253be3391b06124a0e16105bd663b6c54470af1a9ca8e1cf026f38d58ed056
BLAKE2b-256 checksum
How to use checksums
23934e45e37f26c820216704b36b8e67cc55c490a7abd287a78a26a2c0e21386
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 21, 2026.

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