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.

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:

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

You can also 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.1.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

lazy_loader-0.1-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for lazy_loader-0.1.tar.gz
Algorithm Hash digest
SHA256 77ce7f2737ebabf9c0ff73b4a99c947876d74d24c2f026544e32246ecca5feca
MD5 c4e009fd28c9d81899ca594ac0f42662
BLAKE2b-256 e5dd1f5d37840f6a4ebc9822df2c5df19b3c477e02a893598a1eb26aa6cb5806

See more details on using hashes here.

Provenance

File details

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

File metadata

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

File hashes

Hashes for lazy_loader-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 623bd4831a40ce659d74472af40a58d016f2a5a047685409affbc2ba5c044641
MD5 be4daec03e0d7e947312421851ec462f
BLAKE2b-256 bcbf58dbe1f382ecac2c0571c43b6e95028b14e159d67d75e49a00c26ef63d8f

See more details on using hashes here.

Provenance

Supported by

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