Skip to main content

A dependency injection library for Python.

Project description

License Author: David Runemalm Master workflow PyPI version Downloads

py-dependency-injection

A dependency injection library for Python.

Why py-dependency-injection?

py-dependency-injection is inspired by the built-in dependency injection system in ASP.NET Core. It provides a lightweight and extensible way to manage dependencies in Python applications. By promoting constructor injection and supporting scoped lifetimes, it encourages clean architecture and makes testable, maintainable code the default.

Features

  • Scoped Registrations: Define the lifetime of your dependencies as transient, scoped, or singleton.
  • Constructor Injection: Automatically resolve and inject dependencies when creating instances.
  • Method Injection: Inject dependencies into methods using a simple decorator.
  • Factory Functions: Register factory functions, classes, or lambdas to create dependencies.
  • Instance Registration: Register existing instances as dependencies.
  • Tag-Based Registration and Resolution: Organize and resolve dependencies based on tags.
  • Multiple Containers: Support for using multiple dependency containers.

Compatibility

The library is compatible with the following Python versions:

  • 3.7, 3.8, 3.9, 3.10, 3.11, 3.12, 3.13

Installation

$ pip install py-dependency-injection

Quick Start

Here's a quick example to get you started:

from dependency_injection.container import DependencyContainer

# Define an abstract payment gateway interface
class PaymentGateway:
    def charge(self, amount: int, currency: str):
        raise NotImplementedError()

# A concrete implementation using Stripe
class StripeGateway(PaymentGateway):
    def charge(self, amount: int, currency: str):
        print(f"Charging {amount} {currency} using Stripe...")

# A service that depends on the payment gateway
class CheckoutService:
    def __init__(self, gateway: PaymentGateway):
        self._gateway = gateway

    def checkout(self):
        self._gateway.charge(2000, "USD")  # e.g. $20.00

# Get the default dependency container
container = DependencyContainer.get_instance()

# Register StripeGateway as a singleton (shared for the app's lifetime)
container.register_singleton(PaymentGateway, StripeGateway)

# Register CheckoutService as transient (new instance per resolve)
container.register_transient(CheckoutService)

# Resolve and use the service
checkout = container.resolve(CheckoutService)
checkout.checkout()

Documentation

For more advanced usage and examples, please visit our readthedocs page.

License

py-dependency-injection is released under the GPL 3 license. See LICENSE for more details.

Source Code

You can find the source code for py-dependency-injection on GitHub.

Release Notes

Latest: 1.0.0-beta.3 (2025-06-14)

  • Enhancement: Added DependencyContainer.configure_default_container_name(...) to support container isolation in parallel tests, even when application code uses a single shared container via DependencyContainer.get_instance().
  • Enhancement: Added DependencyContainer.clear_instances() as a clean alternative to manually resetting _instances during test teardown.

➡️ Full changelog: GitHub Releases

Project details


Download files

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

Source Distribution

py-dependency-injection-1.0.0b3.tar.gz (20.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py_dependency_injection-1.0.0b3-py3-none-any.whl (22.3 kB view details)

Uploaded Python 3

File details

Details for the file py-dependency-injection-1.0.0b3.tar.gz.

File metadata

  • Download URL: py-dependency-injection-1.0.0b3.tar.gz
  • Upload date:
  • Size: 20.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.8

File hashes

Hashes for py-dependency-injection-1.0.0b3.tar.gz
Algorithm Hash digest
SHA256 1bcfef728fba503a4db4b36116c4b2d0dbb03608d8a2e81848d18a9538cc51b2
MD5 fabb0799cac8352c2954e149ef5be7bb
BLAKE2b-256 ed504eb88f9b022f68fa816a1ab42f684d994ad9e1eefdb54cc656319670d154

See more details on using hashes here.

File details

Details for the file py_dependency_injection-1.0.0b3-py3-none-any.whl.

File metadata

  • Download URL: py_dependency_injection-1.0.0b3-py3-none-any.whl
  • Upload date:
  • Size: 22.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.8

File hashes

Hashes for py_dependency_injection-1.0.0b3-py3-none-any.whl
Algorithm Hash digest
SHA256 4a21aba643d94d2696ab2526a275f298c628bb6ae8e98a20086cba5eac5dc801
MD5 16d1a66f24a2ba6761f81ac1835b6203
BLAKE2b-256 50483bdcd988855d8f1989ae83d5d15fb4f66f64e3c26ca9351860b210a8b10d

See more details on using hashes here.

Supported by

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