
Security News
How Enterprise Security Is Adapting to AI-Accelerated Threats
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.
py-dependency-injection
Advanced tools
A dependency injection library for Python.
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.
This library requires Python 3.9 or later.
It is tested and compatible with:
$ pip install py-dependency-injection
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()
For more advanced usage and examples, please visit our readthedocs page.
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.
You can find the source code for py-dependency-injection on GitHub.
pipenv to Poetry.➡️ Full changelog: GitHub Releases
FAQs
A dependency injection library for Python - inspired by ASP.NET Core.
We found that py-dependency-injection demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.