Dependency injection framework for Python
.. figure:: https://raw.githubusercontent.com/wiki/ets-labs/python-dependency-injector/img/logo.svg :target: https://github.com/ets-labs/python-dependency-injector
.. image:: https://img.shields.io/pypi/v/dependency_injector.svg :target: https://pypi.org/project/dependency-injector/ :alt: Latest Version
.. image:: https://img.shields.io/pypi/l/dependency_injector.svg :target: https://pypi.org/project/dependency-injector/ :alt: License
.. image:: https://img.shields.io/pypi/pyversions/dependency_injector.svg :target: https://pypi.org/project/dependency-injector/ :alt: Supported Python versions
.. image:: https://img.shields.io/pypi/implementation/dependency_injector.svg :target: https://pypi.org/project/dependency-injector/ :alt: Supported Python implementations
.. image:: https://pepy.tech/badge/dependency-injector :target: https://pepy.tech/project/dependency-injector :alt: Downloads
.. image:: https://pepy.tech/badge/dependency-injector/month :target: https://pepy.tech/project/dependency-injector :alt: Downloads
.. image:: https://pepy.tech/badge/dependency-injector/week :target: https://pepy.tech/project/dependency-injector :alt: Downloads
.. image:: https://img.shields.io/pypi/wheel/dependency-injector.svg :target: https://pypi.org/project/dependency-injector/ :alt: Wheel
.. image:: https://img.shields.io/github/workflow/status/ets-labs/python-dependency-injector/Tests%20and%20linters/master :target: https://github.com/ets-labs/python-dependency-injector/actions :alt: Build Status
.. image:: https://coveralls.io/repos/github/ets-labs/python-dependency-injector/badge.svg?branch=master :target: https://coveralls.io/github/ets-labs/python-dependency-injector?branch=master :alt: Coverage Status
Dependency Injectoris a dependency injection framework for Python.
It helps implementing the dependency injection principle.
Key features of the
Selectorproviders that help assembling your objects. See
pydanticsettings, environment variables, and dictionaries. See
Typing and mypy_.
.. code-block:: python
from dependencyinjector import containers, providers from dependencyinjector.wiring import inject, Provide
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.apikey.fromenv('APIKEY') container.config.timeout.fromenv('TIMEOUT') container.wire(modules=[sys.modules[name]])
When you callmain()function theServicedependency is assembled and injected automatically.
When doing a testing you call thecontainer.api_client.override()to replace the real API client with a mock. When you callmain()the mock is injected.
You can override any provider with another provider.
It also helps you in configuring project for the different environments: replace an API client with a stub on the dev or stage.
With theDependency Injectorobjects assembling is consolidated in the container. Dependency injections are defined explicitly. This makes easier to understand and change how application works.
.. figure:: https://raw.githubusercontent.com/wiki/ets-labs/python-dependency-injector/img/di-readme.svg :target: https://github.com/ets-labs/python-dependency-injector
Visit the docs to know more about theDependency injection and inversion of control in Python_.
The package is available on thePyPi_::pip install dependency-injector
The documentation is availablehere_.
Choose one of the following:
Application example (single container)_
Application example (multiple containers)_
Decoupled packages example (multiple containers)_
FastAPI + Redis example_
FastAPI + SQLAlchemy example_
Choose one of the following:
Flask web application tutorial_
Aiohttp REST API tutorial_
Asyncio monitoring daemon tutorial_
CLI application tutorial_
The framework stands on the
PEP20 (The Zen of Python)_ principle:
.. code-block:: bash
Explicit is better than implicit
You need to specify how to assemble and where to inject the dependencies explicitly.
The power of the framework is in a simplicity.
Dependency Injectoris a simple tool for the powerful concept.
What is the dependency injection? - dependency injection is a principle that decreases coupling and increases cohesion
Why should I do the dependency injection? - your code becomes more flexible, testable and clear 😎
How do I start doing the dependency injection? - you start writing the code following the dependency injection principle - you register all of your application components and their dependencies in the container - when you need a component, you specify where to inject it or get it from the container
What price do I pay and what do I get? - you need to explicitly specify the dependencies - it will be extra work in the beginning - it will payoff as the project grows
Have a question? - Open a
Found a bug? - Open a
Want to help? - |star| Star the
Dependency Injectoron the
Github_ - |new| Start a new project with the
Dependency Injector- |tell| Tell your friend about the
Want to contribute? - |fork| Fork the project - |pull| Open a pull request to the
.. _PyPi: https://pypi.org/project/dependency-injector/
.. |star| unicode:: U+2B50 U+FE0F .. star sign1 .. |new| unicode:: U+1F195 .. new sign .. |tell| unicode:: U+1F4AC .. tell sign .. |fork| unicode:: U+1F500 .. fork sign .. |pull| unicode:: U+2B05 U+FE0F .. pull sign