Install & Compatibility
Where this runs
tested against v7.685.25166 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.069s · 17.9MB
glibcpy 3.10–3.920 runs
installs and imports cleanly · install 1.6s · import 0.065s · 18MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
expect
✓ from mo_imports import expect
Used to declare an expected variable in a module that will be provided by another module to break cyclic dependencies.
export
✓ from mo_imports import export
Used to make a variable available to modules that have declared it as 'expected'.
delay_import
✓ from mo_imports import delay_import
Creates a proxy object that imports the specified module variable only upon its first use (e.g., calling, item access, attribute access).
The `mo-imports` library offers two primary patterns: `expect`/`export` for breaking cyclic dependencies and `delay_import` for deferring module loading until the first access. The `delay_import` example demonstrates how a module's content is loaded only when its components are first invoked, leading to cleaner code and potentially faster startup times if imports are expensive and not always used.
import os
# Example for breaking cyclic dependencies with expect/export
# File: foos.py
# from mo_imports import expect
# bar = expect("bar")
# def foo():
# return bar() + " from foo"
#
# File: bars.py
# from mo_imports import export
# from foos import foo
# def bar():
# return foo() + " from bar"
# export("bar", bar)
# Simulating the usage (actual usage requires separate files)
# To demonstrate, imagine 'foos.py' defines 'bar' as expect('bar')
# and 'bars.py' defines 'bar' and then export('bar', bar)
# Example for delayed import
# File: lazy_module.py
# def some_function():
# return "Function from lazy_module"
# File: main_app.py
from mo_imports import delay_import
# Instead of: from lazy_module import some_function
# We use delay_import:
lazy_function = delay_import("lazy_module.some_function")
print(f"Initial state: {lazy_function}") # This is a proxy object, not the actual function yet
# The import happens when lazy_function is first 'used'
# For a function, this means when it's called.
try:
result = lazy_function()
print(f"Result from delayed import: {result}")
except ImportError as e:
print(f"Caught expected ImportError: {e}")
print("Note: For this example to run correctly, 'lazy_module.py' must exist in the Python path.")
# For demonstration, let's create a dummy lazy_module.py temporarily
with open('lazy_module.py', 'w') as f:
f.write('def some_function():\n return "Function from lazy_module"\n')
# Rerun the delayed import to show it working
import sys
# Remove from sys.modules to force re-import if it was loaded before
if 'lazy_module' in sys.modules:
del sys.modules['lazy_module']
lazy_function_working = delay_import("lazy_module.some_function")
print(f"Result from delayed import after creating file: {lazy_function_working()}")
# Clean up the dummy file
os.remove('lazy_module.py')
Errors
Common errors & fixes
NameError: name 'my_variable' is not defined
You are using a variable declared with `mo_imports.expect('my_variable')` in one module, but the module responsible for `mo_imports.export('my_variable', value)` has not yet been imported or executed when 'my_variable' is accessed.
fixReview your module import order. Ensure that the module exporting the variable is imported before the module that expects it. The `expect` call sets up a placeholder, but the actual value is only bound upon `export`.
AttributeError: 'DelayImport' object has no attribute 'some_attribute'
You are attempting to access an attribute or call a method on a `delay_import` proxy object, but the underlying module has not yet been loaded because no triggering operation (call, item access, attribute access) has occurred.
fixEnsure that the `delay_import` proxy is interacted with in a way that triggers the actual import, such as calling it (`obj()`) if it's a function, accessing an item (`obj['key']`) if it's a dictionary-like object, or accessing an attribute (`obj.attr`) if it's a module/class. If the object itself is meant to be a simple value, `delay_import` might not be the appropriate tool.
Upgrade
Version history
7.685.25166latest on PyPI · released Jun 15, 2025
Audit
Dependencies
No dependency data recorded yet.