{"id":4053,"library":"inject","title":"Inject","description":"Inject is a lightweight, fast, and thread-safe Python dependency injection framework. It facilitates the creation and management of object dependencies, promoting modular and testable code. The library supports features like automatic parameter injection using type annotations and context managers. It maintains an active development status, with the current version being 5.3.0, and new releases are made periodically.","status":"active","version":"5.3.0","language":"en","source_language":"en","source_url":"https://github.com/ivankorobkov/python-inject","tags":["dependency injection","DI","inversion of control"],"install":[{"cmd":"pip install inject","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"symbol":"inject","correct":"import inject"},{"note":"While `import inject` and then `inject.autoparams` works, direct import is cleaner if only `autoparams` is needed.","wrong":"import inject.autoparams","symbol":"autoparams","correct":"from inject import autoparams"},{"note":"`inject.param` is deprecated; use `inject.params` for multiple or single parameter injection.","wrong":"from inject import param","symbol":"params","correct":"from inject import params"},{"symbol":"instance","correct":"from inject import instance"}],"quickstart":{"code":"import inject\n\n# 1. Define services/dependencies\nclass Cache:\n    def save(self, key, value):\n        print(f\"Saving to cache: {key} = {value}\")\n\nclass DbInterface:\n    def query(self, statement):\n        print(f\"Executing DB query: {statement}\")\n        return [f\"result_for_{statement}\"]\n\n# 2. Configure bindings (once at application start)\ndef config(binder):\n    binder.bind(Cache, Cache())\n    binder.bind(DbInterface, DbInterface())\n\ninject.configure(config)\n\n# 3. Use dependency injection\n# Method 1: Using inject.instance()\ncache_instance = inject.instance(Cache)\ncache_instance.save('app_init', 'initial_value')\n\n# Method 2: Using @inject.autoparams (requires type hints)\n@inject.autoparams\ndef process_data(data: str, cache: Cache, db: DbInterface):\n    cache.save('processed', data)\n    results = db.query(f\"SELECT * FROM data WHERE value = '{data}'\")\n    print(f\"Processed '{data}', results: {results}\")\n\nprocess_data('example_data')\n\n# Method 3: Using @inject.params (explicitly name dependencies)\n@inject.params(cache=Cache, db=DbInterface)\ndef report_status(message: str, cache=None, db=None):\n    if cache: cache.save('status', message)\n    if db: db.query(f\"INSERT INTO logs VALUES ('{message}')\")\n    print(f\"Status reported: {message}\")\n\nreport_status('Application running successfully')\n","lang":"python","description":"This quickstart demonstrates how to set up and use `inject` for dependency injection. It covers configuring bindings, requesting instances directly with `inject.instance()`, and automatically injecting parameters into functions using `@inject.autoparams` (with type hints) or explicitly with `@inject.params`."},"warnings":[{"fix":"Upgrade your Python environment to 3.9+ or pin `inject` to an appropriate older major version (e.g., `pip install 'inject<5'` for Python 3.5-3.8).","message":"`inject` v5.0 and later require Python 3.9+. If you are using an older Python version (e.g., 3.5-3.8), you must use `inject` v4.x. For Python 2.7-3.5, use `inject` v3.x.","severity":"breaking","affected_versions":"5.0.0+"},{"fix":"Replace `@inject.param('dependency_name', DependencyType)` with `@inject.params(dependency_name=DependencyType)`.","message":"The `inject.param` decorator is deprecated. Use `inject.params` instead for injecting parameters into functions.","severity":"deprecated","affected_versions":"4.1+"},{"fix":"Call `inject.configure(config, bind_in_runtime=False)` to disable implicit runtime binding. This will cause `inject.instance()` or injection to raise an `InjectorException` immediately if a binding is not explicitly provided.","message":"By default, `inject` attempts to implicitly instantiate a class if a binding is missing (`bind_in_runtime=True`). This can mask missing bindings and lead to hard-to-debug issues if the default constructor is called unintentionally. For stricter control, disable runtime binding.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Define and bind custom provider functions (e.g., `binder.bind_to_provider(MyScopedDep, my_custom_scope_provider_func)`) for dependencies that require non-singleton lifetimes.","message":"`inject` creates objects as singletons by default. For advanced scope management (e.g., thread-local, request-local instances), you need to implement custom providers rather than relying on built-in scope management features found in some other DI frameworks.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}