lazyasd
raw JSON → 0.1.4 verified Sat May 09 auth: no python maintenance
Lazy & self-destructive tools for speeding up module imports in Python. Current version 0.1.4. Low release cadence, supports Python 2.7+ and 3.x.
pip install lazyasd Common errors
error AttributeError: module 'lazyasd' has no attribute 'LazyObject' ↓
cause Version older than 0.1.0 or incorrect installation. This error may occur if the library is not properly installed or a very old version is used.
fix
Upgrade to latest version: pip install --upgrade lazyasd
error ImportError: cannot import name 'lazyobject' from 'lazyasd' ↓
cause The decorator `lazyobject` was added in a later version (0.1.0+). Using an older version or misspelling.
fix
Update lazyasd: pip install --upgrade lazyasd. Ensure correct spelling: lazyobject.
Warnings
gotcha LazyObject does not support attribute access until the module is actually loaded. Attempting to access attributes before any triggering access may raise AttributeError. ↓
fix Ensure the lazy object is accessed (e.g., call a function) to trigger module loading before relying on other attributes.
gotcha The lazyobject decorator creates a self-destructive object: after first call, the name is replaced with the result. If the decorated function raises an exception, the object remains, but subsequent calls may behave unexpectedly. ↓
fix Wrap the function body in try-except to ensure cleanup on error, or avoid using lazyobject for code that may fail.
Imports
- LazyObject
from lazyasd import LazyObject - lazyobject
from lazyasd import lazyobject - LazyDict
from lazyasd import LazyDict - load_module_in_background
from lazyasd import load_module_in_background
Quickstart
from lazyasd import LazyObject, lazyobject
# Lazy import via LazyObject
np = LazyObject({}, module='numpy')
# Use np only when accessed; numpy is imported on first use.
# Example: print(np.array([1,2,3]))
# Self-destructive module function (runs once, then deletes itself)
@lazyobject
def some_expensive_setup():
# do heavy initialization
return 'done'
# After first call, some_expensive_setup is replaced with its return value.
print(some_expensive_setup)