Skip to content

Install and First Module

Install

pip install monkay

Minimal Setup

Create a package module that wires Monkay once and exports your lazy API:

yourpkg/__init__.py
import os

from monkay import Monkay

monkay = Monkay(
    # Required for auto-hooking
    globals(),
    with_extensions=True,
    with_instance=True,
    settings_path=lambda: os.environ.get("SETTINGS_MODULE_IMPORT", "settings_path:Settings") or "",
    preloads=["tests.targets.module_full_preloaded1:load"],
    # Warning: settings names have a catch
    settings_preloads_name="preloads",
    settings_extensions_name="extensions",
    uncached_imports=["settings"],
    lazy_imports={
        "bar": "tests.targets.fn_module:bar",
        "settings": lambda: monkay.settings,
    },
    deprecated_lazy_imports={
        "deprecated": {
            "path": "tests.targets.fn_module:deprecated",
            "reason": "old.",
            "new_attribute": "super_new",
        }
    },
)

Then use it from a runtime entrypoint:

yourpkg/main.py
from foo import monkay as foo_monkay


def get_application():
    # sys.path updates
    important_preloads = [...]
    foo_monkay.evaluate_preloads(important_preloads, ignore_import_errors=False)
    extra_preloads = [...]
    foo_monkay.evaluate_preloads(extra_preloads)
    foo_monkay.evaluate_settings()
    return ...


app = get_application()

Verify Runtime Behavior

  1. Access a lazy import and confirm it resolves on first use.
  2. Call evaluate_settings() once startup configuration is ready.
  3. If needed, call evaluate_preloads() explicitly in startup.