py-dependency-injection


Namepy-dependency-injection JSON
Version 1.0.0rc2 PyPI version JSON
download
home_pagehttps://py-dependency-injection.readthedocs.io/en/latest/
SummaryA dependency injection library for Python - inspired by ASP.NET Core.
upload_time2025-08-07 19:40:04
maintainerNone
docs_urlNone
authorDavid Runemalm
requires_python>=3.9
licenseMIT
keywords dependency injection di ioc inversion of control python
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Author: David Runemalm](https://img.shields.io/badge/Author-David%20Runemalm-blue)](https://www.davidrunemalm.com)
[![Python Versions](https://img.shields.io/pypi/pyversions/py-dependency-injection)](https://pypi.org/project/py-dependency-injection/)
[![Master workflow](https://github.com/runemalm/py-dependency-injection/actions/workflows/master.yml/badge.svg?branch=master)](https://github.com/runemalm/py-dependency-injection/actions/workflows/master.yml)
[![PyPI version](https://badge.fury.io/py/py-dependency-injection.svg)](https://pypi.org/project/py-dependency-injection/)
![Downloads](https://pepy.tech/badge/py-dependency-injection)
![No dependencies](https://img.shields.io/badge/dependencies-none-brightgreen)

# 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

```bash
$ pip install py-dependency-injection
```

## Quick Start

Here's a quick example to get you started:

```python
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](https://py-dependency-injection.readthedocs.io/en/latest/) 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](LICENSE) for full details.

## Source Code

You can find the source code for `py-dependency-injection` on [GitHub](https://github.com/runemalm/py-dependency-injection).

## Release Notes

### Latest: [1.0.0-rc.2](https://github.com/runemalm/py-dependency-injection/releases/tag/v1.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](https://github.com/runemalm/py-dependency-injection/releases)

            

Raw data

            {
    "_id": null,
    "home_page": "https://py-dependency-injection.readthedocs.io/en/latest/",
    "name": "py-dependency-injection",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "dependency injection, di, ioc, inversion of control, python",
    "author": "David Runemalm",
    "author_email": "david.runemalm@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/8f/3c/1311377e82d2a21735dde40b00dfaaf0f999bdb641d171dca7bc68a8855c/py_dependency_injection-1.0.0rc2.tar.gz",
    "platform": null,
    "description": "[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n[![Author: David Runemalm](https://img.shields.io/badge/Author-David%20Runemalm-blue)](https://www.davidrunemalm.com)\n[![Python Versions](https://img.shields.io/pypi/pyversions/py-dependency-injection)](https://pypi.org/project/py-dependency-injection/)\n[![Master workflow](https://github.com/runemalm/py-dependency-injection/actions/workflows/master.yml/badge.svg?branch=master)](https://github.com/runemalm/py-dependency-injection/actions/workflows/master.yml)\n[![PyPI version](https://badge.fury.io/py/py-dependency-injection.svg)](https://pypi.org/project/py-dependency-injection/)\n![Downloads](https://pepy.tech/badge/py-dependency-injection)\n![No dependencies](https://img.shields.io/badge/dependencies-none-brightgreen)\n\n# py-dependency-injection\n\nA dependency injection library for Python.\n\n## Why py-dependency-injection?\n\n`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.\n\nThis library is implemented in **pure Python** and has **no runtime dependencies**.\n\n## Features\n\n- **Scoped Registrations:** Define the lifetime of your dependencies as transient, scoped, or singleton.\n- **Constructor Injection:** Automatically resolve and inject dependencies when creating instances.\n- **Method Injection:** Inject dependencies into methods using a simple decorator.\n- **Factory Functions:** Register factory functions, classes, or lambdas to create dependencies.\n- **Instance Registration:** Register existing instances as dependencies.\n- **Tag-Based Registration and Resolution:** Organize and resolve dependencies based on tags.\n- **Multiple Containers:** Support for using multiple dependency containers.\n\n## Compatibility\n\nThis library requires **Python 3.9 or later**.\n\nIt is tested and compatible with:\n\n- Python 3.9, 3.10, 3.11, 3.12, 3.13\n\n## Installation\n\n```bash\n$ pip install py-dependency-injection\n```\n\n## Quick Start\n\nHere's a quick example to get you started:\n\n```python\nfrom dependency_injection.container import DependencyContainer\n\n# Define an abstract payment gateway interface\nclass PaymentGateway:\n    def charge(self, amount: int, currency: str):\n        raise NotImplementedError()\n\n# A concrete implementation using Stripe\nclass StripeGateway(PaymentGateway):\n    def charge(self, amount: int, currency: str):\n        print(f\"Charging {amount} {currency} using Stripe...\")\n\n# A service that depends on the payment gateway\nclass CheckoutService:\n    def __init__(self, gateway: PaymentGateway):\n        self._gateway = gateway\n\n    def checkout(self):\n        self._gateway.charge(2000, \"USD\")  # e.g. $20.00\n\n# Get the default dependency container\ncontainer = DependencyContainer.get_instance()\n\n# Register StripeGateway as a singleton (shared for the app's lifetime)\ncontainer.register_singleton(PaymentGateway, StripeGateway)\n\n# Register CheckoutService as transient (new instance per resolve)\ncontainer.register_transient(CheckoutService)\n\n# Resolve and use the service\ncheckout = container.resolve(CheckoutService)\ncheckout.checkout()\n```\n\n## Documentation\n\nFor more advanced usage and examples, please visit our [readthedocs](https://py-dependency-injection.readthedocs.io/en/latest/) page.\n\n## License\n\n`py-dependency-injection` is released under the MIT license \u2014 a permissive license that allows commercial use, modification, distribution, and private use.\nSee [LICENSE](LICENSE) for full details.\n\n## Source Code\n\nYou can find the source code for `py-dependency-injection` on [GitHub](https://github.com/runemalm/py-dependency-injection).\n\n## Release Notes\n\n### Latest: [1.0.0-rc.2](https://github.com/runemalm/py-dependency-injection/releases/tag/v1.0.0-rc.2) (2025-08-07)\n\n- **License Change**: Switched from GPL-3.0 to MIT to support broader adoption and commercial use.\n- **Toolchange**: Migrated from `pipenv` to `Poetry`.\n- **Dropped Python 3.7 & 3.8 support** \u2013 Both versions are EOL and increasingly unsupported by modern tooling.\n\n\u27a1\ufe0f Full changelog: [GitHub Releases](https://github.com/runemalm/py-dependency-injection/releases)\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A dependency injection library for Python - inspired by ASP.NET Core.",
    "version": "1.0.0rc2",
    "project_urls": {
        "Documentation": "https://py-dependency-injection.readthedocs.io/en/latest/",
        "Homepage": "https://py-dependency-injection.readthedocs.io/en/latest/",
        "Repository": "https://github.com/runemalm/py-dependency-injection"
    },
    "split_keywords": [
        "dependency injection",
        " di",
        " ioc",
        " inversion of control",
        " python"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3bbac4f1be1e284a0fcc4f47381ad16d085e396d5b95d8c48abb2448d264474a",
                "md5": "b2072590981816c9a885a53f7a93a228",
                "sha256": "eb8b81b4e79e873c8cf4d36774be2d72c832ce808fbf17550137abf726e52971"
            },
            "downloads": -1,
            "filename": "py_dependency_injection-1.0.0rc2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b2072590981816c9a885a53f7a93a228",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 10843,
            "upload_time": "2025-08-07T19:40:02",
            "upload_time_iso_8601": "2025-08-07T19:40:02.154011Z",
            "url": "https://files.pythonhosted.org/packages/3b/ba/c4f1be1e284a0fcc4f47381ad16d085e396d5b95d8c48abb2448d264474a/py_dependency_injection-1.0.0rc2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8f3c1311377e82d2a21735dde40b00dfaaf0f999bdb641d171dca7bc68a8855c",
                "md5": "4fedbe1e1f42551eb9499142cc28ce5c",
                "sha256": "27454e797f708b7a40a183effc452950fdd11acac43981959c202f5dca6e1da6"
            },
            "downloads": -1,
            "filename": "py_dependency_injection-1.0.0rc2.tar.gz",
            "has_sig": false,
            "md5_digest": "4fedbe1e1f42551eb9499142cc28ce5c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 8881,
            "upload_time": "2025-08-07T19:40:04",
            "upload_time_iso_8601": "2025-08-07T19:40:04.451951Z",
            "url": "https://files.pythonhosted.org/packages/8f/3c/1311377e82d2a21735dde40b00dfaaf0f999bdb641d171dca7bc68a8855c/py_dependency_injection-1.0.0rc2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-08-07 19:40:04",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "runemalm",
    "github_project": "py-dependency-injection",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "py-dependency-injection"
}
        
Elapsed time: 2.06200s