- Notifications
You must be signed in to change notification settings - Fork0
A dependency injection library for Python.
License
runemalm/py-dependency-injection
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
A dependency injection library for Python.
py-dependency-injection is inspired by the built-in dependency injection system inASP.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 inpure Python and hasno runtime dependencies.
- 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.
This library requiresPython 3.9 or later.
It is tested and compatible with:
- Python 3.9, 3.10, 3.11, 3.12, 3.13
$ pip install py-dependency-injection
Here's a quick example to get you started:
fromdependency_injection.containerimportDependencyContainer# Define an abstract payment gateway interfaceclassPaymentGateway:defcharge(self,amount:int,currency:str):raiseNotImplementedError()# A concrete implementation using StripeclassStripeGateway(PaymentGateway):defcharge(self,amount:int,currency:str):print(f"Charging{amount}{currency} using Stripe...")# A service that depends on the payment gatewayclassCheckoutService:def__init__(self,gateway:PaymentGateway):self._gateway=gatewaydefcheckout(self):self._gateway.charge(2000,"USD")# e.g. $20.00# Get the default dependency containercontainer=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 servicecheckout=container.resolve(CheckoutService)checkout.checkout()
For more advanced usage and examples, please visit ourreadthedocs page.
py-dependency-injection is released under the MIT license — a permissive license that allows commercial use, modification, distribution, and private use.SeeLICENSE for full details.
You can find the source code forpy-dependency-injection onGitHub.
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
pipenvtoPoetry. - Dropped Python 3.7 & 3.8 support – Both versions are EOL and increasingly unsupported by modern tooling.
➡️ Full changelog:GitHub Releases
About
A dependency injection library for Python.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.