# module-inject
module-inject is a Python library designed to inject configuration values into modules before they are imported, enabling centralized and declarative control over runtime behavior across dynamic or plugin-based systems.
This is particularly useful in applications where modules need to be configured externally, but standard import mechanisms do not allow mutation of module-level state prior to execution.
Features
- Inject values into modules before import-time execution
- Fine-grained control with per-module and per-package scopes
- Lazy and dynamic module loading after configuration
- Optional type enforcement and stack-based namespace lookup
Installation
pip install module-inject
Usage
Consider a plugin system where plugins are python modules under a plugins/ directory need to be initialized with context-specific parameters.
Example
main.py
from pathlib import Path
from module_inject import Module
# Create a controller for the package
package = Module.from_import_path("plugins")
# Set a package-scoped variable
package.set(role="slave")
# Set a variable for the current script
Module.from_path(Path(__file__)).set(role="master")
# Iterate over all module files and inject values before importing
for path in Path("plugins").glob("*.py"):
module = Module.from_path(path)
module.set(variable=f"value for {module.import_path}")
module.load().init()
plugins/interesting_plugin.py
import module_inject as module
def init():
role = module.get("role", str, package=True)
print(role)
# Module-level config access
print(module.get("variable"))
print(module.get_namespace())
Output
value for plugins.interesting_plugin
{'variable': 'value for plugins.interesting_plugin'}
slave
API
Module.from_import_path(path: str) -> Module
Creates a Module controller for the given import path (e.g., "plugins.interesting_plugin").
Module.from_path(path: Path) -> Module
Creates a Module controller from a filesystem path to a .py file.
Searches for the file in sys.path under the hood to resolve import path.
module.get(key: str, type_: type[T] = None, package: bool = False, recursive: bool = False) -> T
Retrieves the value associated with key.
If type_ is set - checks if the value in namespace is instance of the provided type.
If package=True, it will attempt to retrieve the value from the package scope rather than the module-specific scope.
If recursive=True, it will walk over the call stack to find the name.
module.get_namespace() -> dict
Returns a dictionary of all key-value pairs injected into the current module.
Module.set(**kwargs) -> None
Sets key-value pairs to be injected into the associated module upon import.
Module.load() -> types.ModuleType
Loads (imports) the target module with the injected configuration. Must be called to finalize injection.
Design Philosophy
This library provides a low-level interface to orchestrate dynamic configuration of Python modules, particularly in systems requiring deferred or context-aware initialization. Unlike environment variables or global config objects, this approach allows values to be injected into module namespace dictionaries before execution.
Metadata
Release files for module-inject 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| module_inject-0.0.1.tar.gz | 3.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| module_inject-0.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 7.6 kB
Release files / module_inject-0.0.1.tar.gz
| Download URL | module_inject-0.0.1.tar.gz |
|---|---|
| Size | 3.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
be20e3e29c98db116eb2a740c4979eacc9bcc69c19caaec3abe56f05d4b64b75
|
|
BLAKE2b-256 checksum How to use checksums |
d6188c0b75d05386cbd881ceea84deec0d239bc81fb69c759ac58bb656b80bc3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.7.15
|
Release files / module_inject-0.0.1-py3-none-any.whl
| Download URL | module_inject-0.0.1-py3-none-any.whl |
|---|---|
| Size | 4.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8fd8a8fc734a1c21d303f6b2eec0fc47ec20f791836ea258ca60705353c5a0c3
|
|
BLAKE2b-256 checksum How to use checksums |
f69db5a03cc070d538d264d3d6c7d3a2599d3e59cb78241ffd8cb5f5afef5c75
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.7.15
|
Security for module-inject 0.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.