Skip to main content

lazy_loader

Project description

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

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

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

lazy_loader-0.3.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

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

lazy_loader-0.3-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file lazy_loader-0.3.tar.gz.

File metadata

  • Download URL: lazy_loader-0.3.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.31.0

File hashes

Hashes for lazy_loader-0.3.tar.gz
Algorithm Hash digest
SHA256 3b68898e34f5b2a29daaaac172c6555512d0f32074f147e2254e4a6d9d838f37
MD5 6b0f19ab63de5d00b862325bbeca7ea7
BLAKE2b-256 0e3a1630a735bfdf9eb857a3b9a53317a1e1658ea97a1b4b39dcb0f71dae81f8

See more details on using hashes here.

File details

Details for the file lazy_loader-0.3-py3-none-any.whl.

File metadata

  • Download URL: lazy_loader-0.3-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.31.0

File hashes

Hashes for lazy_loader-0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1e9e76ee8631e264c62ce10006718e80b2cfc74340d17d1031e0f84af7478554
MD5 a14207a93562ded085306a505f14ba06
BLAKE2b-256 a1c365b3814e155836acacf720e5be3b5757130346670ac454fee29d3eda1381

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