Skip to main content

Lazyimports

logo

PyPI PyPI - License PyPI - Wheel Tests Copier Ruff

Overview 🌐

Lazyimports is a Python module that enables lazy imports using native Python syntax, reducing startup time and improving performance by delaying module loading until needed.

Installation 🔨

Install lazyimports via pip:

pip install auto-lazy-imports

Usage 👍

1. Using a with Statement

Wrap imports in a with statement to enable lazy loading:

import lazyimports

with lazyimports.lazy_imports(catchall=True):
    from package import submodule

submodule.hello()

Note: With catchall enabled, all modules under the with statement will be lazily loaded. However, you can also explicitly specify which packages to load lazily by providing them as arguments. This is especially useful, if you want to use lazy objects

import lazyimports

with lazyimports.lazy_imports("package:function", "package.subpackage"):
    import package.subpackage
    from package import function

package.subpackage.hello()
function()

2. Configuring via pyproject.toml

Define lazy-loaded modules and objects in pyproject.toml for package-based usage.

Standard configuration:

[project.entry-points.lazyimports]
"lazy_modules" = "package,package.submodule"
"lazy_functions" = "package:hello"
"lazy_objects" = "package:array,package:integer"

Poetry-based configuration:

[tool.poetry.plugins.lazyimports]
"lazy_modules" = "package,package.submodule"
"lazy_functions" = "package:hello"
"lazy_objects" = "package:array,package:integer"

💡 The keys (lazy_modules, lazy_functions, etc.) can be listed in any order, using comma-separated values.

The previous example is also equivalent to:

[project.entry-points.lazyimports]
"custom_key" = "package,package.submodule,package:hello,package:array,package:integer"

After defining the configuration, import modules as usual—no code modifications needed:

from package import submodule
from package import hello

3. Using an Environment Variable (for Development)

Dynamically enable lazy imports by setting an environment variable:

export PYTHON_LAZY_IMPORTS="package,package.submodule,package:array,package:integer,package:hello"
python script.py

Advanced Usage 🧑‍🏫

1. Counted Lazy objects

Sometimes you want to use have limit the lazy imports of a certain object to your package. In that cas, you can use counted Lazy objects, which will only lazy import an object a limited number of times.

import lazyimports

with lazyimports.lazy_imports("package:function#3"):
    import package
    from package import function # Lazily Imported: Counter decremented by 2 ( from ... import syntax)

    package.function # Lazily Imported: Counter decremented by 1 (attribute access syntax)

    from package import function # Eagerly Imported: Counter reached 0

function()

2. Shortcut Collection Module

Shortcut collection modules are a special kind of modules, that will import lazy objects from other modules to provide an import shortcut.

If you import a lazy object from a shortcut collection, it will trigger an automatic import.

Here is a common pattern using counted lazy objects and shortcut collection modules:

├───my_package
│   ├───submodule1.py
│   ├───submodule2.py
│   └───__init__.py
└───main.py
# my_package/__init__.py

from lazyimports import lazy_imports, lazy_modules

lazy_modules.add("~my_package") # could also be defined in pyproject.toml

with lazy_imports("my_package.submodule1:MyClass1#2", "my_package.submodule2:MyClass2#2"):
    from .submodule1 import MyClass1
    from .submodule2 import MyClass2

__all__ = ["MyClass1", "MyClass2"]
# main.py

from my_package import MyClass2

# MyClass2 is eagerly loaded ( you do not get a proxy but the real class ),
# but MyClass1 won't be loaded until it is also imported

Metadata

Release files for auto-lazy-imports 0.4.2

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

Source distribution (sdist)

Source distribution for auto-lazy-imports 0.4.2
File Size Uploaded
auto_lazy_imports-0.4.2.tar.gz 213.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for auto-lazy-imports 0.4.2
File Interpreter ABI Platform
auto_lazy_imports-0.4.2-py3-none-any.whl Python 3 none any Details

Total release size: 220.9 kB

Release files / auto_lazy_imports-0.4.2.tar.gz

Download URL auto_lazy_imports-0.4.2.tar.gz
Size 213.6 kB
Tags Source
SHA-256 checksum
How to use checksums
1dfc5e19778e324b447f739ba03642355d2f08e73946724b9dd8cba4c56af398
BLAKE2b-256 checksum
How to use checksums
5d8d9d12579cca0e95cb236d56a2cf312bdcd94709a0cebb56b8d053e25afa3c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

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 Apr 15, 2025.

Transparency log

Release files / auto_lazy_imports-0.4.2-py3-none-any.whl

Download URL auto_lazy_imports-0.4.2-py3-none-any.whl
Size 7.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f1b341b4b0fce4d1dc6e6a84a8ee921a0082296965c8afeb14aeb9639274287f
BLAKE2b-256 checksum
How to use checksums
9b7334ab46d80fa256653b528f7ecad542f5090d032d6c54d9e807b78d49e2d8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

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 Apr 15, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

0.4.2 This release

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

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