Lazy Loader

0.5 · active · verified Sun Mar 29

The `lazy-loader` library makes it easy to load Python subpackages and functions on demand. This utility is designed to help projects, especially in the scientific Python ecosystem, reduce startup time and memory usage by deferring module imports until the imported objects are actually accessed. It is actively maintained, currently at version 0.5, and sees regular updates to enhance functionality and fix bugs.

Warnings

Install

Imports

Quickstart

This example demonstrates how to use `lazy.attach` within a package's `__init__.py` to lazily load submodules. The `heavy_module` is only imported (and its 'imported!' message printed) when one of its attributes is first accessed, not during the initial `import mypackage` statement. This is the primary use case for `lazy-loader` to improve package import times.

import lazy_loader as lazy
import os

# This would typically be in your package's __init__.py
# For demonstration, we simulate it.

# Define a dummy 'heavy_module' that prints when imported
# In a real scenario, this would be a separate file or external library
with open('heavy_module.py', 'w') as f:
    f.write("""print('heavy_module imported!')
def expensive_func():
    return 'Result from expensive_func'
""")

# Create a dummy package directory and __init__.py
os.makedirs('mypackage', exist_ok=True)
with open('mypackage/__init__.py', 'w') as f:
    f.write("""import lazy_loader as lazy

__getattr__, __dir__, _ = lazy.attach(
    __name__, __file__,
    subpackages=[
        'heavy_module', # This will be lazily loaded
    ],
    attributes={
        'my_utility_func': ('some_utility_module', 'my_utility_func') # Example for external functions
    }
)

# If you also wanted to lazy load an external library directly (less common in __init__.py)
# external_lib = lazy.load('sys') # Example: lazily load 'sys'
""")

print("Before importing mypackage")
import mypackage
print("After importing mypackage (heavy_module not yet loaded)")

# Accessing an attribute of mypackage.heavy_module triggers its load
result = mypackage.heavy_module.expensive_func()
print(f"Accessed heavy_module: {result}")

# Clean up dummy files
os.remove('heavy_module.py')
os.remove('mypackage/__init__.py')
os.rmdir('mypackage')

view raw JSON →