Skip to main content

A module to enable lazy imports using native python syntax

Project description

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():
    from package import submodule

submodule.hello()

Note: By default, 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

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

auto_lazy_imports-0.3.0.tar.gz (206.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

auto_lazy_imports-0.3.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file auto_lazy_imports-0.3.0.tar.gz.

File metadata

  • Download URL: auto_lazy_imports-0.3.0.tar.gz
  • Upload date:
  • Size: 206.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for auto_lazy_imports-0.3.0.tar.gz
Algorithm Hash digest
SHA256 3cdc1a78049d5cb7894198f3a9bbdff0c4743700c9092c4f37565c2941513642
MD5 72cb83d5e016ab52dc67342c50c1043f
BLAKE2b-256 fbce40bf74e72b30def5c7cd86a435e9d0e650d2fa088d64bed86431ac92f564

See more details on using hashes here.

Provenance

The following attestation bundles were made for auto_lazy_imports-0.3.0.tar.gz:

Publisher: publish.yaml on hmiladhia/lazyimports

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file auto_lazy_imports-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for auto_lazy_imports-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a4e7e88280ba5f901a494bac2b0592e0a9a1a8163e8e0c476a2a8d6b57cbde92
MD5 ad1ae031f063caa039e90aafea2657ba
BLAKE2b-256 1807827e6027c4d2275a2ba4ba8e8f54a3ad6fadcc5cb26cacfab4445475240f

See more details on using hashes here.

Provenance

The following attestation bundles were made for auto_lazy_imports-0.3.0-py3-none-any.whl:

Publisher: publish.yaml on hmiladhia/lazyimports

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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