A module to enable lazy imports using native python syntax
Project description
Lazyimports
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.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pylazyimports-0.5.3.tar.gz.
File metadata
- Download URL: pylazyimports-0.5.3.tar.gz
- Upload date:
- Size: 201.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7269cd5b9257cebc84322177e415f027227966c7c360a2abe7896d458227a0e9
|
|
| MD5 |
9465fa3dde7274b6297733581c693805
|
|
| BLAKE2b-256 |
1be39a8c2316627b39ffd5fc7e633825386454098427f8a7d24184d7670c4f0a
|
Provenance
The following attestation bundles were made for pylazyimports-0.5.3.tar.gz:
Publisher:
publish.yml on hmiladhia/lazyimports
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pylazyimports-0.5.3.tar.gz -
Subject digest:
7269cd5b9257cebc84322177e415f027227966c7c360a2abe7896d458227a0e9 - Sigstore transparency entry: 1025831600
- Sigstore integration time:
-
Permalink:
hmiladhia/lazyimports@ace5e38d5e7d6a655620128d634a15badb22b124 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/hmiladhia
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@ace5e38d5e7d6a655620128d634a15badb22b124 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pylazyimports-0.5.3-py3-none-any.whl.
File metadata
- Download URL: pylazyimports-0.5.3-py3-none-any.whl
- Upload date:
- Size: 10.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b34b96f8e5bdd4897958651530032e721d88a48366b6628ebfa9678f22c2c698
|
|
| MD5 |
6997b99723f3e3cfde2c7b694f1f57f9
|
|
| BLAKE2b-256 |
ae13872df0cba97f7bcfc61f8595539ad092b630ad53b3aeaf10be579733b0f0
|
Provenance
The following attestation bundles were made for pylazyimports-0.5.3-py3-none-any.whl:
Publisher:
publish.yml on hmiladhia/lazyimports
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pylazyimports-0.5.3-py3-none-any.whl -
Subject digest:
b34b96f8e5bdd4897958651530032e721d88a48366b6628ebfa9678f22c2c698 - Sigstore transparency entry: 1025832459
- Sigstore integration time:
-
Permalink:
hmiladhia/lazyimports@ace5e38d5e7d6a655620128d634a15badb22b124 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/hmiladhia
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@ace5e38d5e7d6a655620128d634a15badb22b124 -
Trigger Event:
push
-
Statement type: