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 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
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 auto_lazy_imports-0.4.0.tar.gz.
File metadata
- Download URL: auto_lazy_imports-0.4.0.tar.gz
- Upload date:
- Size: 213.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
323f3b3a45d5a12f137d67d53a05e6381fa1bf7535b6f3adf92c196e4558f41a
|
|
| MD5 |
108e671967f4eb49ab56c66b9ad7cb3d
|
|
| BLAKE2b-256 |
05c5aea824534d445e467dcf5284a394b6010ef292fb02db8b5b9a50135165aa
|
Provenance
The following attestation bundles were made for auto_lazy_imports-0.4.0.tar.gz:
Publisher:
publish.yaml on hmiladhia/lazyimports
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
auto_lazy_imports-0.4.0.tar.gz -
Subject digest:
323f3b3a45d5a12f137d67d53a05e6381fa1bf7535b6f3adf92c196e4558f41a - Sigstore transparency entry: 196986461
- Sigstore integration time:
-
Permalink:
hmiladhia/lazyimports@f1488975e52d7f81d735c49bde43e77fb031fdc3 -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/hmiladhia
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@f1488975e52d7f81d735c49bde43e77fb031fdc3 -
Trigger Event:
release
-
Statement type:
File details
Details for the file auto_lazy_imports-0.4.0-py3-none-any.whl.
File metadata
- Download URL: auto_lazy_imports-0.4.0-py3-none-any.whl
- Upload date:
- Size: 7.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bffdad28207615b4d8a7ca74ff7aabc03b020d2f0ecc0270b6741832d487bd70
|
|
| MD5 |
5cb99097b2ac9fc5b400ca6b39d1f856
|
|
| BLAKE2b-256 |
ab79bc7eb88310e74d6cf8d87b8783680b82d696ebf4f7548b373d871ba2fecd
|
Provenance
The following attestation bundles were made for auto_lazy_imports-0.4.0-py3-none-any.whl:
Publisher:
publish.yaml on hmiladhia/lazyimports
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
auto_lazy_imports-0.4.0-py3-none-any.whl -
Subject digest:
bffdad28207615b4d8a7ca74ff7aabc03b020d2f0ecc0270b6741832d487bd70 - Sigstore transparency entry: 196986466
- Sigstore integration time:
-
Permalink:
hmiladhia/lazyimports@f1488975e52d7f81d735c49bde43e77fb031fdc3 -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/hmiladhia
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@f1488975e52d7f81d735c49bde43e77fb031fdc3 -
Trigger Event:
release
-
Statement type: