Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Principle:Eventual Inc Daft Lazy Dependency Loading

From Leeroopedia


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:

  1. The proxy object has the same interface as the real module (via `__getattr__`).
  2. The real module is created (imported) only on first access.
  3. Subsequent accesses delegate directly to the cached module.
  4. Pickling support ensures the proxy can be serialized in distributed computing contexts.

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment