Skip to main content

Dead-simple dependency injection framework for Python.

Project description

python-injection

PyPI - Version PyPI - Downloads

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()

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

python_injection-0.26.0.tar.gz (24.6 kB view details)

Uploaded Source

Built Distribution

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

python_injection-0.26.0-py3-none-any.whl (31.4 kB view details)

Uploaded Python 3

File details

Details for the file python_injection-0.26.0.tar.gz.

File metadata

  • Download URL: python_injection-0.26.0.tar.gz
  • Upload date:
  • Size: 24.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","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}

File hashes

Hashes for python_injection-0.26.0.tar.gz
Algorithm Hash digest
SHA256 70ae0327b88a7bde26bfbc35013dc8477f25267467f482b93ce3895a7b1d6612
MD5 81bba878dbea2c499d31834aa34012bd
BLAKE2b-256 e4a392d298bed487d83a49fc2f49b717a5c97cb63056291ee14e3177fb56049c

See more details on using hashes here.

File details

Details for the file python_injection-0.26.0-py3-none-any.whl.

File metadata

  • Download URL: python_injection-0.26.0-py3-none-any.whl
  • Upload date:
  • Size: 31.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","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}

File hashes

Hashes for python_injection-0.26.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fa87dbef570bd0c67755b04296075e5d019af389878e55208c446016e859d90e
MD5 d834009869dffec30b0e30b0be01d028
BLAKE2b-256 5145b5795255ab61a8e42f0b34e542f2ebf3dd8c8d3da4ab09f4fbce8529a97e

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