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 Injector?
Dependency Injectoris a dependency injection framework for Python.
It helps implementing the dependency injection principle.
Key features of the
Dependency Injector:
Factory,
Singleton,
Callable,
Coroutine,
Object,
List,
Dict,
Configuration,
Resource,
Dependencyand
Selectorproviders that help assembling your objects. See
Providers_.
Provider overriding_.
yaml&
inifiles,
pydanticsettings, environment variables, and dictionaries. See
Configuration provider_.
Containers_.
Resource provider_.
Wiring_.
Asynchronous injections_.
mypy-friendly. See
Typing and mypy_.
Cython.
.. code-block:: python
from dependencyinjector import containers, providers from dependencyinjector.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.apikey.fromenv('APIKEY') container.config.timeout.fromenv('TIMEOUT') container.wire(modules=[sys.modules[name]])
main() #When you call
main()function theServicedependency is assembled and injected automatically.When doing a testing you call the
container.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 the
Dependency 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 the
Dependency injection and inversion of control in Python_.Installation
The package is available on the
PyPi_::pip install dependency-injectorDocumentation
The documentation is available
here_.Examples
Choose one of the following:
Application example (single container)_
Application example (multiple containers)_
Decoupled packages example (multiple containers)_
Boto3 example_
Django example_
Flask example_
Aiohttp example_
Sanic example_
FastAPI example_
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
Github Issue_
Found a bug? - Open a
Github Issue_
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
Dependency Injector
Want to contribute? - |fork| Fork the project - |pull| Open a pull request to the
developbranch
.. _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