Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

License: MIT Author: David Runemalm Python Versions Master workflow PyPI version Downloads No dependencies

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.

This library is implemented in pure Python and has no runtime dependencies.

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

This library requires Python 3.9 or later.

It is tested and compatible with:

  • Python 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 MIT license — a permissive license that allows commercial use, modification, distribution, and private use. See LICENSE for full details.

Source Code

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

Release Notes

Latest: 1.0.0-rc.2 (2025-08-07)

  • License Change: Switched from GPL-3.0 to MIT to support broader adoption and commercial use.
  • Toolchange: Migrated from pipenv to Poetry.
  • Dropped Python 3.7 & 3.8 support – Both versions are EOL and increasingly unsupported by modern tooling.

➡️ Full changelog: GitHub Releases

Metadata

Release files for py-dependency-injection 1.0.0rc2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for py-dependency-injection 1.0.0rc2
File Size Uploaded Provenance Actions
py_dependency_injection-1.0.0rc2.tar.gz 8.9 kB Without provenance Details for py_dependency_injection-1.0.0rc2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for py-dependency-injection 1.0.0rc2
File Interpreter ABI Platform Actions
py_dependency_injection-1.0.0rc2-py3-none-any.whl Python 3 none any Details for py_dependency_injection-1.0.0rc2-py3-none-any.whl

Total release size: 19.7 kB

Release files / py_dependency_injection-1.0.0rc2.tar.gz

Download URL py_dependency_injection-1.0.0rc2.tar.gz
Size 8.9 kB
Tags Source
SHA-256 checksum
How to use checksums
27454e797f708b7a40a183effc452950fdd11acac43981959c202f5dca6e1da6
BLAKE2b-256 checksum
How to use checksums
8f3c1311377e82d2a21735dde40b00dfaaf0f999bdb641d171dca7bc68a8855c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.4 CPython/3.11.6 Darwin/22.6.0

Release files / py_dependency_injection-1.0.0rc2-py3-none-any.whl

Download URL py_dependency_injection-1.0.0rc2-py3-none-any.whl
Size 10.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
eb8b81b4e79e873c8cf4d36774be2d72c832ce808fbf17550137abf726e52971
BLAKE2b-256 checksum
How to use checksums
3bbac4f1be1e284a0fcc4f47381ad16d085e396d5b95d8c48abb2448d264474a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.4 CPython/3.11.6 Darwin/22.6.0

Security for py-dependency-injection 1.0.0rc2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page