Skip to main content

Dependency injection framework for Python

Project description

https://raw.githubusercontent.com/wiki/ets-labs/python-dependency-injector/img/logo.svg

Latest Version License Supported Python versions Supported Python implementations Downloads Downloads Downloads Wheel Build Status Coverage Status

What is Dependency Injector?

Dependency Injector is a dependency injection framework for Python.

It helps implement the dependency injection principle.

Key features of the Dependency Injector:

  • Providers. Provides Factory, Singleton, Callable, Coroutine, Object, List, Dict, Configuration, Resource, Dependency, and Selector providers that help assemble your objects. See Providers.

  • Overriding. Can override any provider by another provider on the fly. This helps in testing and configuring dev/stage environment to replace API clients with stubs etc. See Provider overriding.

  • Configuration. Reads configuration from yaml, ini, and json files, pydantic settings, environment variables, and dictionaries. See Configuration provider.

  • Resources. Helps with initialization and configuring of logging, event loop, thread or process pool, etc. Can be used for per-function execution scope in tandem with wiring. See Resource provider.

  • Containers. Provides declarative and dynamic containers. See Containers.

  • Wiring. Injects dependencies into functions and methods. Helps integrate with other frameworks: Django, Flask, Aiohttp, Sanic, FastAPI, etc. See Wiring.

  • Asynchronous. Supports asynchronous injections. See Asynchronous injections.

  • Typing. Provides typing stubs, mypy-friendly. See Typing and mypy.

  • Performance. Fast. Written in Cython.

  • Maturity. Mature and production-ready. Well-tested, documented, and supported.

from dependency_injector import containers, providers
from dependency_injector.wiring import Provide, inject


class Container(containers.DeclarativeContainer):

    config = providers.Configuration()

    api_client = providers.Singleton(
        ApiClient,
        api_key=config.api_key,
        timeout=config.timeout,
    )

    service = providers.Factory(
        Service,
        api_client=api_client,
    )


@inject
def main(service: Service = Provide[Container.service]) -> None:
    ...


if __name__ == "__main__":
    container = Container()
    container.config.api_key.from_env("API_KEY", required=True)
    container.config.timeout.from_env("TIMEOUT", as_=int, default=5)
    container.wire(modules=[__name__])

    main()  # <-- dependency is injected automatically

    with container.api_client.override(mock.Mock()):
        main()  # <-- overridden dependency is injected automatically

When you call the main() function the Service dependency is assembled and injected automatically.

When you do testing, you call the container.api_client.override() method to replace the real API client with a mock. When you call main(), the mock is injected.

You can override any provider with another provider.

It also helps you in a re-configuring project for different environments: replace an API client with a stub on the dev or stage.

With the Dependency Injector, object assembling is consolidated in a container. Dependency injections are defined explicitly. This makes it easier to understand and change how an application works.

https://raw.githubusercontent.com/wiki/ets-labs/python-dependency-injector/img/di-readme.svg

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-injector

Documentation

The documentation is available here.

Examples

Choose one of the following:

Tutorials

Choose one of the following:

Concept

The framework stands on the PEP20 (The Zen of Python) principle:

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 its simplicity. Dependency Injector is a simple tool for the powerful concept.

Frequently asked questions

What is 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 applying 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 project grows

Have a question?
Found a bug?
Want to help?
  • ⭐️ Star the Dependency Injector on the Github

  • 🆕 Start a new project with the Dependency Injector

  • 💬 Tell your friend about the Dependency Injector

Want to contribute?
  • 🔀 Fork the project

  • ⬅️ Open a pull request to the develop branch

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dependency_injector-4.48.1.tar.gz (1.1 MB view details)

Uploaded Source

Built Distributions

dependency_injector-4.48.1-pp311-pypy311_pp73-win_amd64.whl (1.6 MB view details)

Uploaded PyPyWindows x86-64

dependency_injector-4.48.1-pp311-pypy311_pp73-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (1.7 MB view details)

Uploaded PyPymanylinux: glibc 2.17+ ARM64manylinux: glibc 2.28+ ARM64

dependency_injector-4.48.1-pp311-pypy311_pp73-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (1.8 MB view details)

Uploaded PyPymanylinux: glibc 2.28+ x86-64manylinux: glibc 2.5+ x86-64

dependency_injector-4.48.1-pp311-pypy311_pp73-macosx_11_0_arm64.whl (1.7 MB view details)

Uploaded PyPymacOS 11.0+ ARM64

dependency_injector-4.48.1-pp310-pypy310_pp73-win_amd64.whl (1.6 MB view details)

Uploaded PyPyWindows x86-64

dependency_injector-4.48.1-pp310-pypy310_pp73-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (1.7 MB view details)

Uploaded PyPymanylinux: glibc 2.17+ ARM64manylinux: glibc 2.28+ ARM64

dependency_injector-4.48.1-pp310-pypy310_pp73-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (1.8 MB view details)

Uploaded PyPymanylinux: glibc 2.28+ x86-64manylinux: glibc 2.5+ x86-64

dependency_injector-4.48.1-pp310-pypy310_pp73-macosx_11_0_arm64.whl (1.7 MB view details)

Uploaded PyPymacOS 11.0+ ARM64

dependency_injector-4.48.1-cp38-abi3-win_amd64.whl (1.6 MB view details)

Uploaded CPython 3.8+Windows x86-64

dependency_injector-4.48.1-cp38-abi3-win32.whl (1.5 MB view details)

Uploaded CPython 3.8+Windows x86

dependency_injector-4.48.1-cp38-abi3-musllinux_1_2_x86_64.whl (1.9 MB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ x86-64

dependency_injector-4.48.1-cp38-abi3-musllinux_1_2_aarch64.whl (1.8 MB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARM64

dependency_injector-4.48.1-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (1.8 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64manylinux: glibc 2.28+ ARM64

dependency_injector-4.48.1-cp38-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (1.9 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.28+ x86-64manylinux: glibc 2.5+ x86-64

dependency_injector-4.48.1-cp38-abi3-macosx_11_0_arm64.whl (1.6 MB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

File details

Details for the file dependency_injector-4.48.1.tar.gz.

File metadata

  • Download URL: dependency_injector-4.48.1.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for dependency_injector-4.48.1.tar.gz
Algorithm Hash digest
SHA256 1805185e4522effad6d5e348c255d27e80d3f8adc89701daf13d743367392978
MD5 6ffe9f4730016e2082a098d7a2a1d7be
BLAKE2b-256 267c5062c4a7ffd32bf210ff55fab9d279a5beeae350fb09533d3536811e13b6

See more details on using hashes here.

Provenance

The following attestation bundles were made for dependency_injector-4.48.1.tar.gz:

Publisher: publishing.yml on ets-labs/python-dependency-injector

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dependency_injector-4.48.1-pp311-pypy311_pp73-win_amd64.whl.

File metadata

File hashes

Hashes for dependency_injector-4.48.1-pp311-pypy311_pp73-win_amd64.whl
Algorithm Hash digest
SHA256 65d9cf9f4eb31f837ed387210158e0003a4509478de1cbdc56c8439232f22ecd
MD5 97d1634fbed2a86be099b4a7fd068fd9
BLAKE2b-256 43b3aa73fe301cf4dc006d2d6d82b6bf2f9a5776854f20e0aaa1122fa4fd1f2f

See more details on using hashes here.

Provenance

The following attestation bundles were made for dependency_injector-4.48.1-pp311-pypy311_pp73-win_amd64.whl:

Publisher: publishing.yml on ets-labs/python-dependency-injector

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dependency_injector-4.48.1-pp311-pypy311_pp73-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for dependency_injector-4.48.1-pp311-pypy311_pp73-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 79be912a0dedb1341b1400018defca6a9966fcb8d4a84b325623fa57d3c08171
MD5 e9a69cffab6ff2e6d368ba5425107df7
BLAKE2b-256 57a700b2a6e8769f3a5b248edf0b0d503289eb3516fa192f4e4cb368163f4a71

See more details on using hashes here.

Provenance

The following attestation bundles were made for dependency_injector-4.48.1-pp311-pypy311_pp73-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl:

Publisher: publishing.yml on ets-labs/python-dependency-injector

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dependency_injector-4.48.1-pp311-pypy311_pp73-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for dependency_injector-4.48.1-pp311-pypy311_pp73-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 2c23ab17cd3e160de1fc5d78719bf86fbfc81c21c8ea02b43832a6a1e2c8a8d8
MD5 8d2ddba82489937d37f4e41c8a9c6770
BLAKE2b-256 70b731061c32c7d3e1f6c3e1fc71eb37d2ba4134e9bb2e50ad558bbff4aad9fa

See more details on using hashes here.

Provenance

The following attestation bundles were made for dependency_injector-4.48.1-pp311-pypy311_pp73-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl:

Publisher: publishing.yml on ets-labs/python-dependency-injector

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dependency_injector-4.48.1-pp311-pypy311_pp73-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for dependency_injector-4.48.1-pp311-pypy311_pp73-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 51f8d9d78a1a147908ed7929df628d859251a814e6a001973bd96ae2b5648760
MD5 47c66c36600fed756047da8c6e10a906
BLAKE2b-256 04de92b98b96742fbc9c04273729cb14c744a97a8dc2ee3e0d12a0d3cc3945e2

See more details on using hashes here.

Provenance

The following attestation bundles were made for dependency_injector-4.48.1-pp311-pypy311_pp73-macosx_11_0_arm64.whl:

Publisher: publishing.yml on ets-labs/python-dependency-injector

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dependency_injector-4.48.1-pp310-pypy310_pp73-win_amd64.whl.

File metadata

File hashes

Hashes for dependency_injector-4.48.1-pp310-pypy310_pp73-win_amd64.whl
Algorithm Hash digest
SHA256 76774369c7268d5dd211af75abfcb4433d972760be90db342c2de325ee4c24a0
MD5 ff691aed3568183e101660b3cd945e6c
BLAKE2b-256 1626bf4612e9adf60fdbfd97360663d2b39ab17bd4308c7294dcfcd54546c701

See more details on using hashes here.

Provenance

The following attestation bundles were made for dependency_injector-4.48.1-pp310-pypy310_pp73-win_amd64.whl:

Publisher: publishing.yml on ets-labs/python-dependency-injector

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dependency_injector-4.48.1-pp310-pypy310_pp73-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for dependency_injector-4.48.1-pp310-pypy310_pp73-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 a659380386bd236579b7f82f51e97f6074d0c878a10db5b50086000de6ce3c28
MD5 9542741ebf8c0ba79719aff93d47a642
BLAKE2b-256 973d7b16ec2cd0f4e7bba380084a713395d1483baa67e7ac63338f7b8a9a30a8

See more details on using hashes here.

Provenance

The following attestation bundles were made for dependency_injector-4.48.1-pp310-pypy310_pp73-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl:

Publisher: publishing.yml on ets-labs/python-dependency-injector

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dependency_injector-4.48.1-pp310-pypy310_pp73-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for dependency_injector-4.48.1-pp310-pypy310_pp73-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 bb33c6d6b6100564dfaee20f3f76a2756158cceff6499e9d0bca8290f8e5f124
MD5 cd334526de08036ba3c4518b394ddb26
BLAKE2b-256 ea6d7bea5ea904465b4d04c7e3cddf669079d9abb0902d75b05417b5f884c570

See more details on using hashes here.

Provenance

The following attestation bundles were made for dependency_injector-4.48.1-pp310-pypy310_pp73-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl:

Publisher: publishing.yml on ets-labs/python-dependency-injector

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dependency_injector-4.48.1-pp310-pypy310_pp73-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for dependency_injector-4.48.1-pp310-pypy310_pp73-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9a7862987b3dcab5ac4fd82f6bbda55d3b15af1ca7492757c428deccc3720140
MD5 3aa5888142e15c722e15025e9537fb16
BLAKE2b-256 dcb9203a1cb19cc4ed42748dceb53d9cafe42ee34928f2d5c18cbe5f30d6a573

See more details on using hashes here.

Provenance

The following attestation bundles were made for dependency_injector-4.48.1-pp310-pypy310_pp73-macosx_11_0_arm64.whl:

Publisher: publishing.yml on ets-labs/python-dependency-injector

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dependency_injector-4.48.1-cp38-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for dependency_injector-4.48.1-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 572b22b7db9b103718ea52634b5ca1ef763278338310254334f4633a57c9f0e7
MD5 8994234d87ab78f28521469e90b25555
BLAKE2b-256 8c414bf523af7e1b7f367499f8b8709e0e807e9a14c7d1674b0442d7f84403c8

See more details on using hashes here.

Provenance

The following attestation bundles were made for dependency_injector-4.48.1-cp38-abi3-win_amd64.whl:

Publisher: publishing.yml on ets-labs/python-dependency-injector

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dependency_injector-4.48.1-cp38-abi3-win32.whl.

File metadata

File hashes

Hashes for dependency_injector-4.48.1-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 58d4d81f92e3267c331f160cbbb517fd7644b95ee57a0d6e4b01f53a7e437a4a
MD5 817a16ee0001d948da2e91aca4b97a72
BLAKE2b-256 6e8a2edaef77e725dd8b1a625c84cbccb0f445afe58277c7b243cbf58784826a

See more details on using hashes here.

Provenance

The following attestation bundles were made for dependency_injector-4.48.1-cp38-abi3-win32.whl:

Publisher: publishing.yml on ets-labs/python-dependency-injector

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dependency_injector-4.48.1-cp38-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for dependency_injector-4.48.1-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 1994622eae8917138626303b176cba4c74e625ba1e588cb09d673ca175d299a2
MD5 38cd4699a98332814f520ce623ea2ddf
BLAKE2b-256 65f92a408d460eedb264f7ea919754c526c8f3a18c026496cacb7dd6960766d2

See more details on using hashes here.

Provenance

The following attestation bundles were made for dependency_injector-4.48.1-cp38-abi3-musllinux_1_2_x86_64.whl:

Publisher: publishing.yml on ets-labs/python-dependency-injector

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dependency_injector-4.48.1-cp38-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for dependency_injector-4.48.1-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 0506e98440ee6c48fe660016d602961b1b3ecc0a8227838a2221048ed11e2fca
MD5 c738c2bfdf1a9de7c6037c51d3f4582b
BLAKE2b-256 cf6dd2a257402c8c3f7a9c61f1b8a0482ec4373f1ef7fdfe784a91e883506e3b

See more details on using hashes here.

Provenance

The following attestation bundles were made for dependency_injector-4.48.1-cp38-abi3-musllinux_1_2_aarch64.whl:

Publisher: publishing.yml on ets-labs/python-dependency-injector

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dependency_injector-4.48.1-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for dependency_injector-4.48.1-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 1b05a4a980096b53ad90a87965c5450183bfbb8bbe36615d7cea97537086d622
MD5 27c75dd504616f41144aab431d381956
BLAKE2b-256 57eed69c4758a12653edbe6ee15c0bf4195981c9820650a1cfa762cbb838485b

See more details on using hashes here.

Provenance

The following attestation bundles were made for dependency_injector-4.48.1-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl:

Publisher: publishing.yml on ets-labs/python-dependency-injector

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dependency_injector-4.48.1-cp38-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for dependency_injector-4.48.1-cp38-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 ac09f508fa9aee06a036ebf3e3d3b2a210276aba1993e9993cec7f1fdc5fd89e
MD5 6a127dd8ec7565223a7c6893adf175cd
BLAKE2b-256 eaf0d91c9cdabb1f2354762aca588757d1aa341f3cbccbc8636dd2c06acac10b

See more details on using hashes here.

Provenance

The following attestation bundles were made for dependency_injector-4.48.1-cp38-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl:

Publisher: publishing.yml on ets-labs/python-dependency-injector

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dependency_injector-4.48.1-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for dependency_injector-4.48.1-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a6f73011d532f3ea59689aad85c7999be6da3f30393041a745d5861cdcdc02e4
MD5 97c03708b8166c35d077a54b7818a3c8
BLAKE2b-256 92f9c9b77652f724aece8856e281f7a71e5af544049b3c068df70c68868e43be

See more details on using hashes here.

Provenance

The following attestation bundles were made for dependency_injector-4.48.1-cp38-abi3-macosx_11_0_arm64.whl:

Publisher: publishing.yml on ets-labs/python-dependency-injector

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page