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 by delaying module loading until needed and help export public API without losing auto-completion/linting.

Installation 🔨

Install lazyimports via pip:

pip install pylazyimports

Usage 👍

1. Using a with Statement

Wrap imports in a with statement and add at least a package name to enable lazy loading. All of its submodules will also be lazy loaded.

import lazyimports

with lazyimports.lazy_imports("package"):
    from package import submodule

submodule.hello()

💡 Note: With the explicit option enabled, all modules and submodules have to be specified explicitly for them to be lazy imported. Use this when you want more control.

import lazyimports

with lazyimports.lazy_imports("package", "package.subpackage", explicit=True):
    import package.subpackage

package.subpackage.hello()

2. Lazy Objects

To load objects/functions/classes lazily, you need to add them to the import context:

with lazyimports.lazy_imports("package") as ctx:
    ctx.add_objects("package", "function")

    from package import function

result = function()

3. Configuring via pyproject.toml

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

Standard configuration - PEP 621

[project.entry-points.lazyimports]
"lazy_function" = "package:function"
"lazy_array" = "package:array"
"lazy_integer" = "package:integer"

Poetry-based configuration

[tool.poetry.plugins.lazyimports]
"lazy_function" = "package:function"
"lazy_array" = "package:array"
"lazy_integer" = "package:integer"

After defining the configuration, import lazy objects with no need to add them to the context as they are automatically added:

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

Advanced Usage 🧑‍🏫

1. Re-export Modules

Re-Export modules are a special kind of modules, that will import lazy objects from other modules to provide an import "shortcut" ( or in other terms to re-export them again).

This is a common pattern when writing your own package, if you have too many modules and want to expose a simple public api in the __init__.py file.

If you import a lazy object from a re-export module, it will trigger an automatic import and the the user will get the real object not a LazyObjectProxy. This is great if you want your package to behave with packages like pydantic.

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

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

from lazyimports import lazy_imports

with lazy_imports(__package__):
    from .submodule1 import MyClass1
    from .submodule2 import MyClass2

__all__ = ["MyClass1", "MyClass2"]
# pyproject.toml

[project.entry-points.lazyimports]
"my_package.submodule1-MyClass1" = "my_package.submodule1:MyClass1"
"my_package.submodule2-MyClass2" = "my_package.submodule1:MyClass2"

[project.entry-points.lazyexporters]
"my_package_any_name" = "my_package"
# 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

Alternate version

If you are not creating a package, you can achieve the same thing without the pyproject.toml entry-points.

# main.py

from lazyimports import lazy_imports, MType

with lazy_imports() as ctx:  # No module is passed, there is no lazy importing involved.
    ctx.add_module("my_package", MType.Export)

    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

2. Filling entry-points automatically

You do not have to fill the entry-points yourself, as it may be tedious to do every time you change your package. You can use build-plugins to fill them for you.

For example, if you add hatch-lazyimports to your build system ( and enable it ), the plugin will analyze your code and add the lazy objects under a with lazy_imports() statement in the entry-points section.

[project]
dependencies = ["pylazyimports>=0.1.0"]
dynamic = ['entry-points', 'entry-points.lazyimports', 'entry-points.lazyexporters']

[build-system]
requires = ["hatchling", "pylazyimports-eps"]
build-backend = "hatchling.build"

[tool.hatch.metadata.hooks.lazyimports]
enabled = true

⚠️ So far, only hatchling is supported.

Release files for pylazyimports 0.5.3

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

Source distribution (sdist)

Source distribution for pylazyimports 0.5.3
File Size Uploaded
pylazyimports-0.5.3.tar.gz 201.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pylazyimports 0.5.3
File Interpreter ABI Platform
pylazyimports-0.5.3-py3-none-any.whl Python 3 none any Details

Total release size: 212.1 kB

Release files / pylazyimports-0.5.3.tar.gz

Download URL pylazyimports-0.5.3.tar.gz
Size 201.3 kB
Tags Source
SHA-256 checksum
How to use checksums
7269cd5b9257cebc84322177e415f027227966c7c360a2abe7896d458227a0e9
BLAKE2b-256 checksum
How to use checksums
1be39a8c2316627b39ffd5fc7e633825386454098427f8a7d24184d7670c4f0a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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 Mar 4, 2026.

Transparency log

Release files / pylazyimports-0.5.3-py3-none-any.whl

Download URL pylazyimports-0.5.3-py3-none-any.whl
Size 10.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b34b96f8e5bdd4897958651530032e721d88a48366b6628ebfa9678f22c2c698
BLAKE2b-256 checksum
How to use checksums
ae13872df0cba97f7bcfc61f8595539ad092b630ad53b3aeaf10be579733b0f0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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 Mar 4, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.5.3 This release

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.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