Skip to main content

Module value injection

Project description

# 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

module_inject-0.0.1.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

module_inject-0.0.1-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file module_inject-0.0.1.tar.gz.

File metadata

  • Download URL: module_inject-0.0.1.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.7.15

File hashes

Hashes for module_inject-0.0.1.tar.gz
Algorithm Hash digest
SHA256 be20e3e29c98db116eb2a740c4979eacc9bcc69c19caaec3abe56f05d4b64b75
MD5 c8f1f745d897eae86d5114128dd698c7
BLAKE2b-256 d6188c0b75d05386cbd881ceea84deec0d239bc81fb69c759ac58bb656b80bc3

See more details on using hashes here.

File details

Details for the file module_inject-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for module_inject-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8fd8a8fc734a1c21d303f6b2eec0fc47ec20f791836ea258ca60705353c5a0c3
MD5 7cff5e7eede5b160da4d612be8d08267
BLAKE2b-256 f69db5a03cc070d538d264d3d6c7d3a2599d3e59cb78241ffd8cb5f5afef5c75

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page