Lazy Imports

1.2.0 · active · verified Sat Apr 11

lazy-imports is a Python library (current version 1.2.0) designed to facilitate lazy loading of modules and symbols, primarily to reduce application startup times. It provides decorators and functions to defer the actual import process until the imported object is first accessed. The library is actively maintained with updates released as needed.

Warnings

Install

Imports

Quickstart

This example demonstrates how to lazily import a specific function (`loads` from `json`) using `lazy_import`. The `json` module is only loaded into memory when `loads` is first invoked, illustrating the startup performance benefit.

from lazy_imports import lazy_import

# Defer import of 'json' module's 'loads' function
# The 'json' module will only be loaded when 'loads' is first called.
loads = lazy_import("json", "loads")

print("json.loads not yet imported...")
data = loads('{"key": "value"}') # The actual 'json' import happens here
print(f"Data loaded: {data}")

# Example with a module (using the decorator)
# from lazy_imports import lazy_module
# @lazy_module("os")
# def get_path_sep():
#     return os.path.sep

# print(f"Path separator: {get_path_sep()}")

view raw JSON →