{"id":13458880,"url":"https://github.com/ets-labs/python-dependency-injector","last_synced_at":"2025-05-06T02:59:12.265Z","repository":{"id":25347262,"uuid":"28774758","full_name":"ets-labs/python-dependency-injector","owner":"ets-labs","description":"Dependency injection framework for Python","archived":false,"fork":false,"pushed_at":"2025-04-07T11:57:49.000Z","size":40875,"stargazers_count":4322,"open_issues_count":214,"forks_count":323,"subscribers_count":52,"default_branch":"master","last_synced_at":"2025-05-06T02:59:04.671Z","etag":null,"topics":["aiohttp","asyncio","dependency-injection","dependency-injection-container","dependency-injection-framework","design-patterns","factory","flask","flask-application","flask-restful","ioc","ioc-container","python","python-2","python-3","python3","singleton","threadlocal"],"latest_commit_sha":null,"homepage":"https://python-dependency-injector.ets-labs.org/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ets-labs.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.rst","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":"rmk135"}},"created_at":"2015-01-04T13:23:05.000Z","updated_at":"2025-05-06T01:19:38.000Z","dependencies_parsed_at":"2025-04-24T04:08:26.486Z","dependency_job_id":"b711f4d2-a3a6-4d63-a2eb-5fc505308237","html_url":"https://github.com/ets-labs/python-dependency-injector","commit_stats":{"total_commits":1710,"total_committers":29,"mean_commits":58.96551724137931,"dds":"0.14093567251461991","last_synced_commit":"cc2304e46e054ae08dc12995428759fbfb51af10"},"previous_names":[],"tags_count":335,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ets-labs%2Fpython-dependency-injector","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ets-labs%2Fpython-dependency-injector/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ets-labs%2Fpython-dependency-injector/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ets-labs%2Fpython-dependency-injector/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ets-labs","download_url":"https://codeload.github.com/ets-labs/python-dependency-injector/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252612466,"owners_count":21776255,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aiohttp","asyncio","dependency-injection","dependency-injection-container","dependency-injection-framework","design-patterns","factory","flask","flask-application","flask-restful","ioc","ioc-container","python","python-2","python-3","python3","singleton","threadlocal"],"created_at":"2024-07-31T09:00:59.242Z","updated_at":"2025-05-06T02:59:12.208Z","avatar_url":"https://github.com/ets-labs.png","language":"Python","readme":".. figure:: https://raw.githubusercontent.com/wiki/ets-labs/python-dependency-injector/img/logo.svg\n   :target: https://github.com/ets-labs/python-dependency-injector\n\n| \n\n.. image:: https://img.shields.io/pypi/v/dependency_injector.svg\n   :target: https://pypi.org/project/dependency-injector/\n   :alt: Latest Version\n   \n.. image:: https://img.shields.io/pypi/l/dependency_injector.svg\n   :target: https://pypi.org/project/dependency-injector/\n   :alt: License\n\n.. image:: https://img.shields.io/pypi/pyversions/dependency_injector.svg\n   :target: https://pypi.org/project/dependency-injector/\n   :alt: Supported Python versions\n   \n.. image:: https://img.shields.io/pypi/implementation/dependency_injector.svg\n   :target: https://pypi.org/project/dependency-injector/\n   :alt: Supported Python implementations\n\n.. image:: https://pepy.tech/badge/dependency-injector\n   :target: https://pepy.tech/project/dependency-injector\n   :alt: Downloads\n\n.. image:: https://pepy.tech/badge/dependency-injector/month\n   :target: https://pepy.tech/project/dependency-injector\n   :alt: Downloads\n\n.. image:: https://pepy.tech/badge/dependency-injector/week\n   :target: https://pepy.tech/project/dependency-injector\n   :alt: Downloads\n\n.. image:: https://img.shields.io/pypi/wheel/dependency-injector.svg\n   :target: https://pypi.org/project/dependency-injector/\n   :alt: Wheel\n\n.. image:: https://img.shields.io/github/actions/workflow/status/ets-labs/python-dependency-injector/tests-and-linters.yml?branch=master\n   :target: https://github.com/ets-labs/python-dependency-injector/actions\n   :alt: Build Status\n\n.. image:: https://coveralls.io/repos/github/ets-labs/python-dependency-injector/badge.svg?branch=master\n   :target: https://coveralls.io/github/ets-labs/python-dependency-injector?branch=master\n   :alt: Coverage Status\n\nWhat is ``Dependency Injector``?\n================================\n\n``Dependency Injector`` is a dependency injection framework for Python.\n\nIt helps implement the dependency injection principle.\n\nKey features of the ``Dependency Injector``:\n\n- **Providers**. Provides ``Factory``, ``Singleton``, ``Callable``, ``Coroutine``, ``Object``,\n  ``List``, ``Dict``, ``Configuration``, ``Resource``, ``Dependency``, and ``Selector`` providers\n  that help assemble your objects.\n  See `Providers \u003chttps://python-dependency-injector.ets-labs.org/providers/index.html\u003e`_.\n- **Overriding**. Can override any provider by another provider on the fly. This helps in testing\n  and configuring dev/stage environment to replace API clients with stubs etc. See\n  `Provider overriding \u003chttps://python-dependency-injector.ets-labs.org/providers/overriding.html\u003e`_.\n- **Configuration**. Reads configuration from ``yaml``, ``ini``, and ``json`` files, ``pydantic`` settings,\n  environment variables, and dictionaries.\n  See `Configuration provider \u003chttps://python-dependency-injector.ets-labs.org/providers/configuration.html\u003e`_.\n- **Resources**. Helps with initialization and configuring of logging, event loop, thread\n  or process pool, etc. Can be used for per-function execution scope in tandem with wiring.\n  See `Resource provider \u003chttps://python-dependency-injector.ets-labs.org/providers/resource.html\u003e`_.\n- **Containers**. Provides declarative and dynamic containers.\n  See `Containers \u003chttps://python-dependency-injector.ets-labs.org/containers/index.html\u003e`_.\n- **Wiring**. Injects dependencies into functions and methods. Helps integrate with\n  other frameworks: Django, Flask, Aiohttp, Sanic, FastAPI, etc.\n  See `Wiring \u003chttps://python-dependency-injector.ets-labs.org/wiring.html\u003e`_.\n- **Asynchronous**. Supports asynchronous injections.\n  See `Asynchronous injections \u003chttps://python-dependency-injector.ets-labs.org/providers/async.html\u003e`_.\n- **Typing**. Provides typing stubs, ``mypy``-friendly.\n  See `Typing and mypy \u003chttps://python-dependency-injector.ets-labs.org/providers/typing_mypy.html\u003e`_.\n- **Performance**. Fast. Written in ``Cython``.\n- **Maturity**. Mature and production-ready. Well-tested, documented, and supported.\n\n.. code-block:: python\n\n   from dependency_injector import containers, providers\n   from dependency_injector.wiring import Provide, inject\n\n\n   class Container(containers.DeclarativeContainer):\n\n       config = providers.Configuration()\n\n       api_client = providers.Singleton(\n           ApiClient,\n           api_key=config.api_key,\n           timeout=config.timeout,\n       )\n\n       service = providers.Factory(\n           Service,\n           api_client=api_client,\n       )\n\n\n   @inject\n   def main(service: Service = Provide[Container.service]) -\u003e None:\n       ...\n\n\n   if __name__ == \"__main__\":\n       container = Container()\n       container.config.api_key.from_env(\"API_KEY\", required=True)\n       container.config.timeout.from_env(\"TIMEOUT\", as_=int, default=5)\n       container.wire(modules=[__name__])\n\n       main()  # \u003c-- dependency is injected automatically\n\n       with container.api_client.override(mock.Mock()):\n           main()  # \u003c-- overridden dependency is injected automatically\n\nWhen you call the ``main()`` function the ``Service`` dependency is assembled and injected automatically.\n\nWhen you do testing, you call the ``container.api_client.override()`` method to replace the real API\nclient with a mock. When you call ``main()``, the mock is injected.\n\nYou can override any provider with another provider.\n\nIt also helps you in a re-configuring project for different environments: replace an API client\nwith a stub on the dev or stage.\n\nWith the ``Dependency Injector``, object assembling is consolidated in a container. Dependency injections are defined explicitly.\nThis makes it easier to understand and change how an application works.\n\n.. figure:: https://raw.githubusercontent.com/wiki/ets-labs/python-dependency-injector/img/di-readme.svg\n   :target: https://github.com/ets-labs/python-dependency-injector\n\nVisit the docs to know more about the\n`Dependency injection and inversion of control in Python \u003chttps://python-dependency-injector.ets-labs.org/introduction/di_in_python.html\u003e`_.\n\nInstallation\n------------\n\nThe package is available on the `PyPi`_::\n\n    pip install dependency-injector\n\nDocumentation\n-------------\n\nThe documentation is available `here \u003chttps://python-dependency-injector.ets-labs.org/\u003e`_.\n\nExamples\n--------\n\nChoose one of the following:\n\n- `Application example (single container) \u003chttps://python-dependency-injector.ets-labs.org/examples/application-single-container.html\u003e`_\n- `Application example (multiple containers) \u003chttps://python-dependency-injector.ets-labs.org/examples/application-multiple-containers.html\u003e`_\n- `Decoupled packages example (multiple containers) \u003chttps://python-dependency-injector.ets-labs.org/examples/decoupled-packages.html\u003e`_\n- `Boto3 example \u003chttps://python-dependency-injector.ets-labs.org/examples/boto3.html\u003e`_\n- `Django example \u003chttps://python-dependency-injector.ets-labs.org/examples/django.html\u003e`_\n- `Flask example \u003chttps://python-dependency-injector.ets-labs.org/examples/flask.html\u003e`_\n- `Aiohttp example \u003chttps://python-dependency-injector.ets-labs.org/examples/aiohttp.html\u003e`_\n- `Sanic example \u003chttps://python-dependency-injector.ets-labs.org/examples/sanic.html\u003e`_\n- `FastAPI example \u003chttps://python-dependency-injector.ets-labs.org/examples/fastapi.html\u003e`_\n- `FastAPI + Redis example \u003chttps://python-dependency-injector.ets-labs.org/examples/fastapi-redis.html\u003e`_\n- `FastAPI + SQLAlchemy example \u003chttps://python-dependency-injector.ets-labs.org/examples/fastapi-sqlalchemy.html\u003e`_\n\nTutorials\n---------\n\nChoose one of the following:\n\n- `Flask web application tutorial \u003chttps://python-dependency-injector.ets-labs.org/tutorials/flask.html\u003e`_\n- `Aiohttp REST API tutorial \u003chttps://python-dependency-injector.ets-labs.org/tutorials/aiohttp.html\u003e`_\n- `Asyncio monitoring daemon tutorial \u003chttps://python-dependency-injector.ets-labs.org/tutorials/asyncio-daemon.html\u003e`_\n- `CLI application tutorial \u003chttps://python-dependency-injector.ets-labs.org/tutorials/cli.html\u003e`_\n\nConcept\n-------\n\nThe framework stands on the `PEP20 (The Zen of Python) \u003chttps://www.python.org/dev/peps/pep-0020/\u003e`_ principle:\n\n.. code-block:: bash\n\n   Explicit is better than implicit\n\nYou need to specify how to assemble and where to inject the dependencies explicitly.\n\nThe power of the framework is in its simplicity.\n``Dependency Injector`` is a simple tool for the powerful concept.\n\nFrequently asked questions\n--------------------------\n\nWhat is dependency injection?\n - dependency injection is a principle that decreases coupling and increases cohesion\n\nWhy should I do the dependency injection?\n - your code becomes more flexible, testable, and clear 😎\n\nHow do I start applying the dependency injection?\n - you start writing the code following the dependency injection principle\n - you register all of your application components and their dependencies in the container\n - when you need a component, you specify where to inject it or get it from the container\n\nWhat price do I pay and what do I get?\n - you need to explicitly specify the dependencies\n - it will be extra work in the beginning\n - it will payoff as project grows\n\nHave a question?\n - Open a `Github Issue \u003chttps://github.com/ets-labs/python-dependency-injector/issues\u003e`_\n\nFound a bug?\n - Open a `Github Issue \u003chttps://github.com/ets-labs/python-dependency-injector/issues\u003e`_\n\nWant to help?\n - |star| Star the ``Dependency Injector`` on the `Github \u003chttps://github.com/ets-labs/python-dependency-injector/\u003e`_\n - |new| Start a new project with the ``Dependency Injector``\n - |tell| Tell your friend about the ``Dependency Injector``\n\nWant to contribute?\n - |fork| Fork the project\n - |pull| Open a pull request to the ``develop`` branch\n\n.. _PyPi: https://pypi.org/project/dependency-injector/\n\n.. |star| unicode:: U+2B50 U+FE0F .. star sign1\n.. |new| unicode:: U+1F195 .. new sign\n.. |tell| unicode:: U+1F4AC .. tell sign\n.. |fork| unicode:: U+1F500 .. fork sign\n.. |pull| unicode:: U+2B05 U+FE0F .. pull sign\n","funding_links":["https://github.com/sponsors/rmk135"],"categories":["Software","Python","Containers \u0026 Language Extentions \u0026 Linting","General Utilities","python3"],"sub_categories":["DI Frameworks / Containers","For Python"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fets-labs%2Fpython-dependency-injector","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fets-labs%2Fpython-dependency-injector","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fets-labs%2Fpython-dependency-injector/lists"}