{"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":"python","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,"search_vec":"'5.3.0':54 'activ':46 'annot':39 'automat':34 'code':28 'context':41 'control':66 'creation':18 'current':51 'depend':12,23,61 'develop':47 'di':63 'facilit':16 'fast':6 'featur':32 'framework':14 'inject':1,2,13,36,62 'invers':64 'librari':30 'lightweight':5 'like':33 'made':59 'maintain':44 'manag':20,42 'modular':25 'new':56 'object':22 'paramet':35 'period':60 'promot':24 'python':11 'releas':57 'safe':10 'status':48 'support':31 'testabl':27 'thread':9 'thread-saf':8 'type':38 'use':37 'version':52","created_at":"2026-04-12T03:38:58.860031+00:00","updated_at":"2026-04-16T15:44:38.563655+00:00","problems":[{"fix":"Ensure the 'inject' library is installed for your active Python environment using pip: `pip install inject`.","cause":"The 'inject' library is not installed in the Python environment being used, or the Python interpreter cannot find it due to incorrect environment configuration (e.g., virtual environment not activated, or package installed in a different Python version).","error":"ModuleNotFoundError: No module named 'inject'"},{"fix":"Verify that all dependencies are correctly bound in the `inject.configure` function using `binder.bind(DependencyType, to=DependencyImplementation)` or `binder.bind_to_provider` or `binder.bind_to_constructor`. Ensure that the types in your annotations match the registered bindings.","cause":"This error often occurs when a dependency expected to be injected by `inject` is not properly bound or resolved, leading to `None` being injected instead of the intended object. When a method is called on this `None` object, an `AttributeError` is raised.","error":"AttributeError: 'NoneType' object has no attribute 'some_method'"},{"fix":"Explicitly bind the missing dependency in your `inject.configure` function, for example: `binder.bind(YourDependency, to=YourDependencyImplementation)`. Alternatively, if runtime binding is desired, remove `bind_in_runtime=False` from `inject.configure` (though this is generally not recommended for robust applications).","cause":"This error occurs when `inject.configure(bind_in_runtime=False)` has been set, disabling automatic (runtime) binding, and `inject` attempts to provide an instance for a class or interface that has not been explicitly bound using a `binder`.","error":"InjectorException: No binding for <class 'your_module.YourDependency'>"},{"fix":"Use `@inject.autoparams` on the `__init__` method of a class for constructor injection. For other methods, consider injecting dependencies into the constructor and then using `self.dependency` within the method, or explicitly define the dependencies as arguments to the method if they are context-specific and then call `inject.instance()` or `inject.params()` if method injection is truly needed and supported for that specific case. The `inject` library generally favors constructor injection for class dependencies.","cause":"The `@inject.autoparams` decorator is typically used for functions or `__init__` methods of classes to automatically inject dependencies based on type hints. Applying it directly to a regular class method might lead to unexpected behavior or `TypeError` if not used as intended by the library to resolve its own parameters.","error":"TypeError: 'type' object is not callable (when using @inject.autoparams on a class method)"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"5.5.0","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/ivankorobkov/python-inject","docs":null,"changelog":null,"pypi":"https://pypi.org/project/inject/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["web-framework"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-29","next_check":"2026-07-28","install_tag":null}}