Skip to main content

ci Docs pypi version Python Versions PyPI - Downloads coverage GitHub Sponsors

lazy-imports-lite changes the semantics of python imports and defers the import until it is used the first time.

It is also important to note that this project is not affiliated in any way with the original PEP. I liked the idea of lazy-imports and wanted to use them for my projects without having to change my code. I hope that this project makes it possible to collect knowledge about

Installation

You can install "lazy-imports-lite" via pip:

pip install lazy-imports-lite

Key Features

  • lazy imports almost like PEP 690
  • no code changes required
  • can be enabled per module (by keyword in the package)

How is it different to PEP 690?

  • It has not the same performance like the implementation from the pep. Every access to parts of imported modules is transformed to an attribute access x -> x.v.
  • Exceptions during deferred import are converted to LazyImportError

Usage

  • add lazy-imports-lite to your project dependencies.
  • add lazy-imports-lite-enabled to the keywords of your project.
      #pyproject.toml
      [project]
      keywords=["lazy-imports-lite-enabled"]
    

This enables lazy imports for all top level imports in your modules in your project. One way to verify if it is enabled is to check which loader is used.

>>> import test_pck
>>> print(type(test_pck.__spec__.loader))
<class 'lazy_imports_lite._loader.LazyLoader'>

Implementation

lazy-imports-lite works by rewriting the AST at runtime before the code is compiled.

The following code:

from foo import bar


def f():
    print(bar())
    print(bar())

is internally transformed to:

import lazy_imports_lite._hooks as __lazy_imports_lite__

globals = __lazy_imports_lite__.make_globals(lambda g=globals: g())
bar = __lazy_imports_lite__.ImportFrom(__package__, "foo", "bar")


def f():
    print(bar.v())
    print(bar.v())

This transformation should be never visible to you (the original source location is preserved) but it is good to know if something does not work as expected.

You can view a preview of this transformation with lazy-imports-lite preview <filename>, if you want to know how your code would be changed.

Issues

If you encounter any problems, please report an issue along with a detailed description.

License

Distributed under the terms of the MIT license, "lazy-imports-lite" is free and open source software.

Metadata

Release files for lazy-imports-lite 0.0.1

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

Source distribution (sdist)

Source distribution for lazy-imports-lite 0.0.1
File Size Uploaded Provenance Actions
lazy_imports_lite-0.0.1.tar.gz 65.5 kB Without provenance Details for lazy_imports_lite-0.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for lazy-imports-lite 0.0.1
File Interpreter ABI Platform Actions
lazy_imports_lite-0.0.1-py3-none-any.whl Python 3 none any Details for lazy_imports_lite-0.0.1-py3-none-any.whl

Total release size: 73.4 kB

Release files / lazy_imports_lite-0.0.1.tar.gz

Download URL lazy_imports_lite-0.0.1.tar.gz
Size 65.5 kB
Tags Source
SHA-256 checksum
How to use checksums
7beca1ac09e52f3c7b62b7248632dc5527bc266f79e09bd507747b56cd96f665
BLAKE2b-256 checksum
How to use checksums
c3e806243a1ad4b224b22d1ee2aaa55fdaac71bdb27b16e33e85287fad002c67
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.25.1

Release files / lazy_imports_lite-0.0.1-py3-none-any.whl

Download URL lazy_imports_lite-0.0.1-py3-none-any.whl
Size 7.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
03e7f57bf6e54b6bde4c60bc948be8ef5fd6633421b248c2372ccd4928073f06
BLAKE2b-256 checksum
How to use checksums
920f045f8e0f227bd2277b3237c60e178842dcc80b0f75e62b842aafbddcf0f8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.25.1

Security for lazy-imports-lite 0.0.1

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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