Dependency Injector is a Python framework that wires objects through providers, containers, and explicit injection into functions and methods.
Overview
Dependency Injector helps implement dependency injection in Python applications. It concentrates object assembly in containers and makes dependency wiring explicit rather than implicit across a codebase.
Core pieces include provider types such as Factory, Singleton, Callable, Coroutine, Configuration, Resource, and Selector; declarative and dynamic containers; configuration loaded from YAML, INI, JSON, environment variables, or Pydantic settings; provider overriding for tests and environments; and wiring that injects dependencies into functions and methods.
The framework supports asynchronous injections, typing stubs for mypy, and integration examples with Django, Flask, Aiohttp, Sanic, and FastAPI. Provider implementations use Cython for performance. The package installs from PyPI via pip.
Tutorials and examples cover single- and multi-container applications, CLI programs, and asyncio daemons. The design emphasizes explicit configuration of how dependencies are created and where they are injected.
Key features
- Factory, Singleton, and other providers
- Declarative and dynamic containers
- Function and method wiring with @inject
- Configuration from files and environment
- Provider overriding for tests
- Resource providers for pools and loops
- Async injection support
- Framework integration examples
Use cases
- Structuring large Python applications
- Testable service composition
- FastAPI or Flask app wiring
- CLI and asyncio program setup