python-injection
Project motivations
Dependency injection in Python has long been a source of frustration.
Existing solutions are often verbose, require extensive boilerplate, or fail to leverage Python's type hints effectively. python-injection was created to solve these problems once and for all by providing a simple, elegant, and powerful dependency injection framework that feels natural to Python developers.
The goal is straightforward: make dependency injection so easy that you'll wonder how you ever managed without it.
Why choose python-injection?
- Type-driven resolution: Dependencies are automatically resolved using Python's type annotations.
- Decorator-based registration: Register your dependencies with simple, readable decorators.
- Flexible lifetimes: Choose from 4 type of lifetimes to match your needs:
- Transient: A new instance every time
- Singleton: One instance for the entire application
- Scoped: One instance per scope, with context manager support
- Constant: Register pre-existing values
- Profile support: Ability to swap certain dependencies based on a profile.
- Pull-based instantiation: Dependencies are only created when needed, improving startup time and resource usage.
- Full sync/async support: Works seamlessly with both synchronous and asynchronous code.
Installation
Requires Python 3.12 or higher.
pip install python-injection
Quick start
Simply apply the decorators and the package takes care of the rest.
from injection import injectable, inject, singleton
@singleton
class Printer:
def __init__(self):
self.history = []
def print(self, message: str):
self.history.append(message)
print(message)
@injectable
class Service:
def __init__(self, printer: Printer):
self.printer = printer
def hello(self):
self.printer.print("Hello world!")
@inject
def main(service: Service):
service.hello()
if __name__ == "__main__":
main()
Release files for python-injection 0.26.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| python_injection-0.26.3.tar.gz | 24.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| python_injection-0.26.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 56.5 kB
Release files / python_injection-0.26.3.tar.gz
| Download URL | python_injection-0.26.3.tar.gz |
|---|---|
| Size | 24.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
81fb5687bc4d733482747a30c0b07aeaa01da632946008f7e2e98d91fe5d6346
|
|
BLAKE2b-256 checksum How to use checksums |
6c2080554fd4ef052b199cdca26897fcb8563bfcab483aa38e37eedcd5bccf7b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.32 {"installer":{"name":"uv","version":"0.11.32","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / python_injection-0.26.3-py3-none-any.whl
| Download URL | python_injection-0.26.3-py3-none-any.whl |
|---|---|
| Size | 31.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f34bcd219701cdb67b5c2d12cc9b49e5375e22edc794c010cc7342fc58a932b2
|
|
BLAKE2b-256 checksum How to use checksums |
8c81a042e57edd76d6f72bc9b61353a376131abe85736c952d6aaec76f2fda42
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.32 {"installer":{"name":"uv","version":"0.11.32","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|