Principle:Eventual Inc Daft Lazy Dependency Loading
| Knowledge Sources | |
|---|---|
| Domains | Import_Management, Performance |
| Last Updated | 2026-02-08 14:00 GMT |
Overview
Pattern that defers module imports until first use to reduce startup time and avoid hard dependencies on optional packages.
Description
Lazy Dependency Loading uses a proxy object that intercepts attribute access and triggers the actual `importlib.import_module` call only when the module is first used. This avoids the overhead of importing large packages (e.g., Ray, database drivers, ML frameworks) at library initialization time, and allows the library to function without optional dependencies installed, failing only when the missing dependency is actually needed.
Usage
Apply this principle for any optional or heavy dependency that is not needed on every code path. It is essential for libraries with many optional integrations where users install only the features they need.
Theoretical Basis
Lazy loading implements the Virtual Proxy pattern from object-oriented design:
- The proxy object has the same interface as the real module (via `__getattr__`).
- The real module is created (imported) only on first access.
- Subsequent accesses delegate directly to the cached module.
- Pickling support ensures the proxy can be serialized in distributed computing contexts.