Dependency Injector — Dependency injection framework for Python¶
Dependency Injector
is a dependency injection framework for Python.
It helps implementing the dependency injection principle.
Key features of the Dependency Injector
:
Providers. Provides
Factory
,Singleton
,Callable
,Coroutine
,Object
,List
,Dict
,Configuration
,Resource
,Dependency
andSelector
providers that help assembling your objects. See Providers.Overriding. Can override any provider by another provider on the fly. This helps in testing and configuring dev / stage environment to replace API clients with stubs etc. See Provider overriding.
Configuration. Reads configuration from
yaml
&ini
files,pydantic
settings, environment variables, and dictionaries. See Configuration provider.Resources. Helps with initialization and configuring of logging, event loop, thread or process pool, etc. Can be used for per-function execution scope in tandem with wiring. See Resource provider.
Containers. Provides declarative and dynamic containers. See Containers.
Wiring. Injects dependencies into functions and methods. Helps integrating with other frameworks: Django, Flask, Aiohttp, Sanic, FastAPI, etc. See Wiring.
Asynchronous. Supports asynchronous injections. See Asynchronous injections.
Typing. Provides typing stubs,
mypy
-friendly. See Typing and mypy.Performance. Fast. Written in
Cython
.Maturity. Mature and production-ready. Well-tested, documented and supported.
from dependency_injector import containers, providers
from dependency_injector.wiring import inject, Provide
class Container(containers.DeclarativeContainer):
config = providers.Configuration()
api_client = providers.Singleton(
ApiClient,
api_key=config.api_key,
timeout=config.timeout.as_int(),
)
service = providers.Factory(
Service,
api_client=api_client,
)
@inject
def main(service: Service = Provide[Container.service]):
...
if __name__ == '__main__':
container = Container()
container.config.api_key.from_env('API_KEY')
container.config.timeout.from_env('TIMEOUT')
container.wire(modules=[sys.modules[__name__]])
main() # <-- dependency is injected automatically
with container.api_client.override(mock.Mock()):
main() # <-- overridden dependency is injected automatically
With the Dependency Injector
objects assembling is consolidated in the container.
Dependency injections are defined explicitly.
This makes easier to understand and change how application works.
Explore the documentation to know more about the Dependency Injector
.
Contents¶
- Introduction
- Examples
- Tutorials
- Providers
- Factory provider
- Singleton provider
- Callable provider
- Coroutine provider
- Object provider
- List provider
- Dict provider
- Configuration provider
- Resource provider
- Selector provider
- Dependency provider
- Provider overriding
- Injecting provided object attributes, items, or call its methods
- Injecting container “self”
- Creating a custom provider
- Asynchronous injections
- Typing and mypy
- Containers
- Wiring
- Other examples
- API Documentation
- Feedback
- Changelog
- 4.29.0
- 4.28.1
- 4.28.0
- 4.27.0
- 4.26.0
- 4.25.1
- 4.25.0
- 4.24.0
- 4.23.5
- 4.23.4
- 4.23.3
- 4.23.2
- 4.23.1
- 4.23.0
- 4.22.1
- 4.22.0
- 4.21.0
- 4.20.2
- 4.20.1
- 4.20.0
- 4.19.0
- 4.18.0
- 4.17.0
- 4.16.0
- 4.15.0
- 4.14.0
- 4.13.2
- 4.13.1
- 4.13.0
- 4.12.0
- 4.11.3
- 4.11.2
- 4.11.1
- 4.11.0
- 4.10.3
- 4.10.2
- 4.10.1
- 4.10.0
- 4.9.1
- 4.9.0
- 4.8.3
- 4.8.2
- 4.8.1
- 4.8.0
- 4.7.0
- 4.6.1
- 4.6.0
- 4.5.4
- 4.5.3
- 4.5.2
- 4.5.1
- 4.5.0
- 4.4.1
- 4.4.0
- 4.3.9
- 4.3.8
- 4.3.7
- 4.3.6
- 4.3.5
- 4.3.4
- 4.3.3
- 4.3.2
- 4.3.1
- 4.3.0
- 4.2.0
- 4.1.8
- 4.1.7
- 4.1.6
- 4.1.5
- 4.1.4
- 4.1.3
- 4.1.2
- 4.1.1
- 4.1.0
- 4.0.6
- 4.0.5
- 4.0.4
- 4.0.3
- 4.0.2
- 4.0.1
- 4.0.0
- 3.44.0
- 3.43.1
- 3.43.0
- 3.42.0
- 3.41.0
- 3.40.0
- 3.39.0
- 3.38.1
- 3.38.0
- 3.37.0
- 3.36.0
- 3.35.1
- 3.35.0
- 3.34.0
- 3.33.0
- 3.32.3
- 3.32.2
- 3.32.1
- 3.32.0
- 3.31.0
- 3.30.4
- 3.30.3
- 3.30.2
- 3.30.1
- 3.30.0
- 3.29.0
- 3.28.1
- 3.28.0
- 3.27.0
- 3.26.0
- 3.25.1
- 3.25.0
- 3.24.1
- 3.24.0
- 3.23.2
- 3.23.1
- 3.23.0
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