Skip to main content

lazyexport

Lazy package exports from ordinary import declarations. Extracted from AnyIO's lazy importer, under its MIT license. Requires Python 3.11 or later and has no runtime dependencies.

from typing import TYPE_CHECKING
from lazyexport import install, fix_package_names

if TYPE_CHECKING or not install():
    from ._client import Client as Client

    fix_package_names()

Place this in your package's __init__.py (ordinary modules also work). install() reads the calling module's source and builds a map of exports from the imports in the conditional block. It installs module-level __getattr__ and __dir__ hooks and returns True, skipping the eager imports. Accessing an export imports its implementation and caches the value in the calling module. If source is unavailable, it returns False and the imports execute eagerly. fix_package_names() makes implementation module names public in that fallback. The lazy path also rewrites exported objects' __module__ to the exporting module.

Supported declarations

Use direct from module import name statements inside the top-level if TYPE_CHECKING or not install(): block. Relative imports and export aliases are supported. from . import submodule exposes submodules lazily. Star imports, plain import module declarations, and nested import declarations are unsupported. Use the unaliased helper names shown here.

For deprecated names, import set_deprecated_aliases from lazyexport and call it inside the block with a literal dictionary mapping old names to fully qualified replacement names. Access emits DeprecationWarning. Lazy resolutions are cached; the eager fallback warns on each access.

The installer replaces existing __getattr__ and __dir__ hooks. dir() lists mapped attribute exports. Define __all__ explicitly if you need star exports; star importing resolves those exports eagerly. References to missing names inside the exporting module do not trigger __getattr__; use this for exports consumed through module attribute access or from imports.

Development

python -m pip install -e '.[test]'
python -m pytest

Releasing

Set project.version in pyproject.toml to the release version and commit it, including .github/workflows/release.yml. Push the commit, then push a matching version tag:

git tag 0.1.0
git push origin 0.1.0

The release workflow tests the package, builds and checks the wheel and source distribution, and publishes them to PyPI using trusted publishing. The tag must match the package version exactly (no v prefix). Configure the PyPI trusted publisher with this repository and workflow filename release.yml, leaving the environment field empty. No API token is required.

Metadata

Release files for lazyexport 0.1.0

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

Source distribution (sdist)

Source distribution for lazyexport 0.1.0
File Size Uploaded Provenance Actions
lazyexport-0.1.0.tar.gz 5.7 kB With provenance Details for lazyexport-0.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 11.6 kB

Release files / lazyexport-0.1.0.tar.gz

Download URL lazyexport-0.1.0.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
d0fdc8f1a6a8b23525d664841a92a839139508e8b2df23095f14753671fe9ab6
BLAKE2b-256 checksum
How to use checksums
edfec8be3982186c1fc1782dbb7226a8f6b3b269785840942b209fc88ef2c833
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 11, 2026.

Transparency log

Release files / lazyexport-0.1.0-py3-none-any.whl

Download URL lazyexport-0.1.0-py3-none-any.whl
Size 5.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3f9b3732bfd44ffd9a6709886d84ea0a6cefb48578ad6790f3632309d770f8ac
BLAKE2b-256 checksum
How to use checksums
66d0296a963161c4534593f923143a9ee33f2e66c71c04a18caaaa5b2dbcf5a4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 11, 2026.

Transparency log

Security for lazyexport 0.1.0

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for lazyexport-0.1.0.tar.gz

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 Nov 11, 2026.

Transparency log

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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

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