Skip to main content

lazyload

Eliminate Python startup latency caused by heavy, eager imports.

PyPI version Python Versions License: MIT Coverage

lazyload defers Python module loading until the exact moment an attribute is accessed for the first time, delivering instant application startup with zero configuration.


The Problem

Standard Python imports execute eagerly at module load time. If your CLI tool or web service imports PyTorch, Pandas, or NumPy at the top of the file, Python parses C-extensions, allocates memory, and runs module initialization code immediately—even when executing simple flags like --help or handling fast paths that never use those dependencies. Loading heavy libraries eagerly turns what should be a sub-10ms CLI command into a frustating 4-second wait.

# Standard eager imports pay 4,000ms startup cost on EVERY execution
import torch
import pandas as pd
import numpy as np

def main():
    if "--help" in sys.argv:
        print_help()  # Loaded PyTorch just to print text!
        return
    model = torch.load("model.pt")

The Solution

lazyload replaces eager module loading with deferred proxies that register in sys.modules instantly. One line of code defers all import work until an attribute is actually accessed during execution. If an execution branch never uses the imported library, the import overhead is eliminated completely.

# One line changes everything — <15ms startup time
import lazyload

torch = lazyload.lazy("torch")
pd = lazyload.lazy("pandas")
np = lazyload.lazy("numpy")

Running --help now returns in 12 milliseconds.


Features

  • Universal Python Support: Works across Python 3.10, 3.11, 3.12, 3.13, 3.14, and 3.15+.
  • Zero External Dependencies: Lightweight pure-Python implementation.
  • Three Import Styles: Support for explicit function deferral (lazy), context blocks (lazy_imports), and function decorators (@lazy_module).
  • Native 3.15 Fast Path: Automatically leverages Python 3.15+ native PEP 810 lazy module capabilities for interpreter-level execution.
  • Compatible Shim for Older Versions: Provides an identical deferred proxy pattern for Python 3.10–3.14.
  • Drop-in Integration: No codebase restructuring or module layout changes required.

Installation

pip install lazyload-py

Quick Start

1. Single Module Deferral (lazy)

Defer a single heavy library by passing its module name as a string.

import lazyload

# Registers a deferred proxy instantly (<0.01ms)
torch = lazyload.lazy("torch")

# PyTorch is loaded here, on first attribute access
tensor = torch.tensor([1.0, 2.0, 3.0])

2. Grouped Import Block (lazy_imports)

Defer multiple standard import statements inside a clean context manager block without altering your import syntax.

import lazyload

# All imports declared inside the block are deferred automatically
with lazyload.lazy_imports():
    import numpy as np
    import pandas as pd
    import scipy

# Execution continues instantly; modules load on first attribute lookup
df = pd.DataFrame({"data": [1, 2, 3]})

3. Function Decorator (lazy_module)

Decorate entry points or CLI handlers to defer all top-level imports inside the function until the function is called for the first time.

import lazyload

@lazyload.lazy_module
def run_training_pipeline():
    # Imports inside the function are deferred until execution
    import torch
    import torchvision

    print("Pipeline started.")

How It Works

On Python 3.15 and above, lazyload delegates directly to the Python interpreter's native PEP 810 lazy module mechanism, leveraging internal engine hooks to eliminate proxy wrapping overhead and allowing the interpreter itself to handle deferred module evaluation.

On Python 3.10 through 3.14, lazyload installs a lightweight proxy object into sys.modules under the target module name. The proxy transparently intercepts attribute lookups, performs the real import on first access, replaces itself in sys.modules with the real module, and updates its internal attribute dictionary so subsequent lookups carry zero ongoing overhead.


Benchmarks

Measured on a CLI entry point importing PyTorch, Pandas, and NumPy on Python 3.10:

Import Approach Startup Time Deferred Overhead Speedup
Eager Standard (import torch, pandas, numpy) 4,112 ms 0 ms 1.0×
lazyload Proxy (lazyload.lazy(...)) 12 ms 4,100 ms 342.6×

Run the benchmark suite locally using python benchmarks/bench_startup.py and python benchmarks/bench_overhead.py.


Contributing

Contributions are welcome! Please review CONTRIBUTING.md for developer setup, code guidelines, and testing procedures.


License

lazyload is licensed under the MIT License.

Release files for lazyload-py 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 lazyload-py 0.1.0
File Size Uploaded
lazyload_py-0.1.0.tar.gz 32.8 kB Details

Built distribution (wheel)

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

Total release size: 67.6 kB

Release files / lazyload_py-0.1.0.tar.gz

Download URL lazyload_py-0.1.0.tar.gz
Size 32.8 kB
Tags Source
SHA-256 checksum
How to use checksums
edaea54ec9af3a043c880998d68c9246e27dbd0e8c08cda6ed199719f857a453
BLAKE2b-256 checksum
How to use checksums
382a4da85ea28c6988d9b1f07bb63012b850daa5007ee032ab8589a698f29e75
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 20, 2026.

Transparency log

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

Download URL lazyload_py-0.1.0-py3-none-any.whl
Size 34.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
34a9a05b323de9da5b50433251fcce5a9037d4553ff33c2ba8c94f3fe38bb03f
BLAKE2b-256 checksum
How to use checksums
dfced5f3ac6578981356ace5a759c6c97f73bc096ea0d6516ef943ad670f4fda
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 20, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

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