Xmod: Turn any object into a module
Xmod is a tiny Python library designed to transform any Python object into a module. This allows for modules to be callable, indexable, or to expose an object's methods and properties directly, reducing boilerplate code. It is currently at version 1.9.0 (released February 3, 2026) and maintains an active development status with frequent minor updates and dependency bumps.
Warnings
- gotcha When explicitly providing the `name` argument to `xmod()`, it should almost always be set to `__name__`. Using a different string can lead to unexpected module aliasing or inconsistent behavior within `sys.modules`.
- gotcha Combining `xmod` with `functools.partial` can sometimes lead to unexpected behavior. Since version `1.3.2`, `xmod` will raise a clear exception if `functools.partial` is used in a way it doesn't support, to prevent silent failures.
- gotcha Using `xmod(..., full=True)` extends the module with *all* members of the extension object, which might unintentionally overwrite existing module attributes. Additionally, `xmod(..., mutable=True)` allows runtime modification of the module's attributes via the `xmod` proxy, potentially introducing hard-to-debug state changes.
Install
-
pip install xmod
Imports
- xmod
import xmod
Quickstart
# In your_module.py
import xmod
@xmod
def greet():
return 'Hello from your_module!'
# To test in another script or interpreter:
# import your_module
# print(your_module())
# Expected output: Hello from your_module!