dependency-injector
Dependency injection framework for Python
What it is and what it does
Dependency Injector is a framework that centralizes how your application assembles and injects object dependencies. Instead of scattering object creation and wiring throughout your code, you declare all components and their relationships in a container, then use decorators or explicit calls to inject them where needed. This makes testing easier (you can override components with mocks), configuration cleaner (read from yaml, json, ini, environment, or pydantic), and the dependency graph explicit and auditable.
The framework provides multiple provider types—Factory for creating new instances, Singleton for shared instances, Configuration for settings, Resource for lifecycle management (logging, event loops, thread pools), and Callable/Coroutine for wrapping functions. It integrates with Django, Flask, FastAPI, Aiohttp, and other frameworks via a wiring system that automatically injects dependencies into decorated functions and methods. Written in Cython for performance, it supports both synchronous and asynchronous injection and includes mypy-friendly typing stubs.
Use it for:
- Organize Flask or FastAPI applications by declaring all services (database, cache, API clients) in a container and injecting them into route handlers
- Replace real API clients with mocks during testing by overriding providers without changing application code
- Manage configuration across dev, stage, and production environments by reading from different sources and swapping implementations
- Initialize and manage resources like database connections, thread pools, or event loops with automatic cleanup
- Build decoupled multi-package applications where each package defines its own container and dependencies
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Dependency Injector is a dependency injection framework that assembles and injects object dependencies into functions and methods, supporting factories, singletons, configuration providers, and async injection.
Yes. Dependency Injector is a mature, actively maintained framework (4907 stars, release 57 days ago, production-stable classifier) with zero known vulnerabilities, permissive licensing, and broad platform support. Install friction is moderate due to Cython compilation but well-mitigated by pre-built wheels. It is worth installing if you want explicit, testable dependency management in a non-trivial application; less essential for simple scripts or if you prefer implicit dependency resolution.
Install
dependency-injector on PyPI
pip
pip install dependency-injectoruv
uv add dependency-injectorpoetry
poetry add dependency-injectorInstalling dependency-injector
Before you install
Medium install friction due to compiled Cython wheels, but well-supported across platforms (Windows, macOS, Linux, including ARM64 and musl). Active maintenance with release 57 days ago and 4907 GitHub stars indicate stable, production-ready status.
License in practice
BSD 3-Clause permissive license allows commercial use, modification, and distribution with minimal restrictions—standard for open-source frameworks.
Quickstart
pip install dependency-injector
from dependency_injector import containers, providers
from dependency_injector.wiring import Provide, inject
class Container(containers.DeclarativeContainer):
service = providers.Singleton(MyService)
@inject
def main(service: MyService = Provide[Container.service]) -> None:
service.do_work()
container = Container()
container.wire(modules=[__name__])
main()
Verify before relying
- Whether Cython compilation overhead affects startup time in short-lived processes or serverless environments
- Performance comparison with other DI frameworks in high-throughput scenarios
- Maturity of async injection support relative to synchronous injection
Package facts
| License | Copyright (c) 2024, Roman Mogylatov All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:… (full text in the JSON record) (permissive) |
| Python support | supports the current Python release (>=3.8) |
| Install friction | medium — platform-specific wheel |
| Runtime dependencies | 1 — typing-extensions |
| Maintenance | actively maintained — 57 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 8,389,683/month — #1,628 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: dependency_injector-4.49.1-cp310-abi3-macosx_11_0_arm64.whl; dependency_injector-4.49.1-cp310-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl; dependency_injector-4.49.1-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl; dependency_injector-4.49.1-cp310-abi3-musllinux_1_2_aarch64.whl; dependency_injector-4.49.1-cp310-abi3-musllinux_1_2_x86_64.whl; dependency_injector-4.49.1-cp310-abi3-win32.whl; dependency_injector-4.49.1-cp310-abi3-win_amd64.whl; dependency_injector-4.49.1-cp38-cp38-macosx_11_0_arm64.whl; dependency_injector-4.49.1-cp38-cp38-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl; dependency_injector-4.49.1-cp38-cp38-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl; dependency_injector-4.49.1-cp38-cp38-musllinux_1_2_aarch64.whl; dependency_injector-4.49.1-cp38-cp38-musllinux_1_2_x86_64.whl; dependency_injector-4.49.1-cp38-cp38-win32.whl; dependency_injector-4.49.1-cp38-cp38-win_amd64.whl; dependency_injector-4.49.1-cp39-cp39-macosx_11_0_arm64.whl; dependency_injector-4.49.1-cp39-cp39-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl; dependency_injector-4.49.1-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl; dependency_injector-4.49.1-cp39-cp39-musllinux_1_2_aarch64.whl; dependency_injector-4.49.1-cp39-cp39-musllinux_1_2_x86_64.whl; dependency_injector-4.49.1-cp39-cp39-win32.whl
Keywords: Dependency injection, DI, Inversion of Control, IoC, Factory, Singleton, Design patterns, Flask
Tags
More Software Development packages
Provides backported and experimental type hints…
permissive · top 100 on PyPI
numpyNumPy provides an N-dimensional array object…
permissive · top 100 on PyPI
fastapiFastAPI is a Python web framework for building…
permissive · top 100 on PyPI
annotated-docProvides a way to document function parameters,…
permissive · top 100 on PyPI
typerTyper builds command-line applications from…
permissive · top 1,000 on PyPI
distlibDistlib provides low-level packaging utilities…
permissive · top 1,000 on PyPI
in-n-outLightweight dependency injection framework…
permissive · top 15,000 on PyPI
injectorInjector is a Python dependency injection…
permissive · top 5,000 on PyPI
lagomLagom is a lightweight dependency injection…
permissive · top 15,000 on PyPI
picoboxPicobox is a lightweight dependency injection…
permissive · top 15,000 on PyPI
rodiRodi is a dependency injection container for…
permissive · top 15,000 on PyPI
simple-diA strictly typed dependency injection library…
permissive · top 15,000 on PyPI
slackA dependency injection container that registers…
permissive · top 15,000 on PyPI
wireupWireup is a type-driven dependency injection…
permissive · top 15,000 on PyPI
dishkadishka is an IoC container that manages…
permissive · top 15,000 on PyPI
punqPunq is a dependency injection container for…
unclear · top 15,000 on PyPI