I am using dependency injection every single day at work with python. Automation stuff, a bit of API work as well. Over 40 classes written, all using dependency injection. Why? It makes things easier to refactor if it is so required(and the project I am working on was started from scratch, so in the beginning a lot of refactoring was required). At first we were not using it, instantiating class fields directly inside the __init__ block. It is safe to say that it was hell to keep track of what class is using what class after a while. Extracted all initialization outside of classes, injected instances into constructors. Easier to read the code now and if I ever need some special kind of treatment for a task, I can write another class with same methods, different implementations and inject it wherever I need. Answer from ItsmeFizzy97 on reddit.com
🌐
PyPI
pypi.org › project › dependency-injector
dependency-injector · PyPI
With the Dependency Injector, object assembling is consolidated in a container. Dependency injections are defined explicitly. This makes it easier to understand and change how an application works. Visit the docs to know more about the Dependency injection and inversion of control in Python.
      » pip install dependency-injector
    
Published   Jun 18, 2026
Version   4.49.1
🌐
Ets-labs
python-dependency-injector.ets-labs.org
Dependency Injector — Dependency injection framework for Python — Dependency Injector 4.49.1 documentation
Dependency Injector is a dependency injection framework for Python. It helps to maintain you application structure. It was designed to be unified, developer-friendly tool that helps to implement dependency injection design pattern in formal, pretty, Pythonic way.
🌐
Medium
medium.com › @spraneeth4 › python-dependency-injector-simplifying-dependency-injection-in-your-projects-14385af0bf78
Python Dependency Injector: Simplifying Dependency Injection in Your Projects | by praneeth_vvs | Medium
September 27, 2024 - A DI framework helps organize and manage these dependencies systematically, making the code cleaner and maintenance easier. Dependency Injector is a lightweight, comprehensive DI tool for Python that simplifies management of application components and their dependencies.
🌐
Reddit
reddit.com › r/python › a new take on dependency injection in python
r/Python on Reddit: A new take on dependency injection in Python
October 11, 2024 -

In case anyone's interested, I've put together a DI framework "pylayer" in python that's fairly different from the alternatives I'm aware of (there aren't many). It includes a simple example at the bottom.
https://gist.github.com/johnhungerford/ccb398b666fd72e69f6798921383cb3f

What my project does

It allows you automatically construct dependencies based on their constructors.

The way it works is you define your dependencies as dataclasses inheriting from an Injectable class, where upstream dependencies are declared as dataclass attributes with type hints. Then you can just pass the classes to an Env object, which you can query for any provided type that you want to use. The Env object will construct a value of that type based on the Injectable classes you have provided. If any dependency needed to construct the queried type, it will generate an error message explaining what was missing and why it was needed.

Target audience

This is a POC that might be of interest to anyone who is uses or has wanted to use dependency injection in a Python project.

Comparison

https://python-dependency-injector.ets-labs.org/ is but complicated and unintuitive. pylayer is more automated and less verbose.

https://github.com/google/pinject is not maintained and seems similarly complicated.

https://itnext.io/dependency-injection-in-python-a1e56ab8bdd0 provides an approach similar to the first, but uses annotations to simplify some aspects of it. It's still more verbose and less intuitive, in my opinion, than pylayer.

Unlike all the above, pylayer has a relatively simple, functional mechanism for wiring dependencies. It is able to automate more by using the type introspection and the automated __init__ provided by dataclasses.

For anyone interested, my approach is based on Scala's ZIO library. Like ZIO's ZLayer type, pylayer takes a functional approach that uses memoization to prevent reconstruction of the same values. The main difference between pylayer and ZIO is that wiring and therefore validation is done at runtime. (Obviously compile-time validation isn't possible in Python...)

🌐
GitConnected
levelup.gitconnected.com › why-i-started-using-dependency-injection-in-python-bff304fb851b
Why I Started Using Dependency Injection in Python | by Haymang Ahuja | Level Up Coding
November 13, 2025 - The simplest form of DI in python is just passing dependencies through constructors (as we saw above). This is called constructor injection and works great for simple cases. But in larger applications, manually writing dependencies gets messy. That’s where the dependency-injector library shines.
Find elsewhere
Top answer
1 of 11
90

See Raymond Hettinger - Super considered super! - PyCon 2015 for an argument about how to use super and multiple inheritance instead of DI. If you don't have time to watch the whole video, jump to minute 15 (but I'd recommend watching all of it).

Here is an example of how to apply what's described in this video to your example:

Framework Code:

class TokenInterface():
    def getUserFromToken(self, token):
        raise NotImplementedError

class FrameworkClass(TokenInterface):
    def do_the_job(self, ...):
        # some stuff
        self.user = super().getUserFromToken(...)

Client Code:

class SQLUserFromToken(TokenInterface):
    def getUserFromToken(self, token):      
        # load the user from the database
        return user

class ClientFrameworkClass(FrameworkClass, SQLUserFromToken):
    pass

framework_instance = ClientFrameworkClass()
framework_instance.do_the_job(...)

This will work because the Python MRO will guarantee that the getUserFromToken client method is called (if super() is used). The code will have to change if you're on Python 2.x.

One added benefit here is that this will raise an exception if the client does not provide a implementation.

Of course, this is not really dependency injection, it's multiple inheritance and mixins, but it is a Pythonic way to solve your problem.

2 of 11
25

The way we do dependency injection in our project is by using the inject lib. Check out the documentation. I highly recommend using it for DI. It kinda makes no sense with just one function but starts making lots of sense when you have to manage multiple data sources etc, etc.

Following your example it could be something similar to:

# framework.py
class FrameworkClass():
    def __init__(self, func):
        self.func = func

    def do_the_job(self):
        # some stuff
        self.func()

Your custom function:

# my_stuff.py
def my_func():
    print('aww yiss')

Somewhere in the application you want to create a bootstrap file that keeps track of all the defined dependencies:

# bootstrap.py
import inject
from .my_stuff import my_func

def configure_injection(binder):
    binder.bind(FrameworkClass, FrameworkClass(my_func))

inject.configure(configure_injection)

And then you could consume the code this way:

# some_module.py (has to be loaded with bootstrap.py already loaded somewhere in your app)
import inject
from .framework import FrameworkClass

framework_instance = inject.instance(FrameworkClass)
framework_instance.do_the_job()

I'm afraid this is as pythonic as it can get (the module has some python sweetness like decorators to inject by parameter etc - check the docs), as python does not have fancy stuff like interfaces or type hinting.

So to answer your question directly would be very hard. I think the true question is: does python have some native support for DI? And the answer is, sadly: no.

🌐
Ets-labs
python-dependency-injector.ets-labs.org › introduction › di_in_python.html
Dependency injection and inversion of control in Python — Dependency Injector 4.49.1 documentation
This page describes the advantages of applying dependency injection in Python. It contains Python examples that show how to implement dependency injection. It demonstrates the usage of the Dependency Injector framework, its container, Factory, Singleton, and Configuration providers.
🌐
DataCamp
datacamp.com › tutorial › python-dependency-injection
Python Dependency Injection: A Guide for Cleaner Code Design | DataCamp
May 7, 2026 - Dependency injection is based on something called the principle of Inversion of Control (IoC), which means that instead of a class creating and managing its dependencies, it receives them from an external source. This helps to focus on a single task and makes your code clean.
🌐
PyPI
pypi.org › project › inject
inject · PyPI
# Import the inject module. import inject # `inject.instance` requests dependencies from the injector. def foo(bar): cache = inject.instance(Cache) cache.save('bar', bar) # `inject.params` injects dependencies as keyword arguments or positional argument. # Also you can use @inject.autoparams in Python 3.5, see the example above.
      » pip install inject
    
Published   Jun 20, 2025
Version   5.3.0
🌐
Code Like A Girl
code.likeagirl.io › dependency-injection-python-18d28ed1c2bc
Dependency Injection- Python. In this tutorial, you will learn how to… | by Python Code Nemesis | Code Like A Girl
April 14, 2023 - In this tutorial, you got more insights into creating containers with the dependency injector Python library, monitoring changes to the config file and creating objects from config file data.
🌐
GeeksforGeeks
geeksforgeeks.org › python › what-is-a-pythonic-way-for-dependency-injection
What is a Pythonic Way for Dependency Injection? - GeeksforGeeks
July 15, 2024 - Each providing unique features and capabilities, several libraries can assist control dependency injection in Python programmes. Popular library Dependency Injector gives a complete framework for DI in Python.
🌐
Xygeni
xygeni.io › home › blog › python dependency injection: how to do it safely
Python Dependency Injection: How to Do It Safely | Xygeni
June 12, 2025 - It’s a design pattern where components like services, clients, or connectors are passed into a class from the outside, instead of being created within it. When used correctly, Python dependency injection allows for better control over external dependencies, making applications easier to test and harder to compromise.
🌐
Ets-labs
python-dependency-injector.ets-labs.org › wiring.html
Wiring — Dependency Injector 4.49.0 documentation
from dependency_injector import containers, providers from dependency_injector.wiring import Provide, inject class Service: ... class Container(containers.DeclarativeContainer): service = providers.Factory(Service) @inject def main(container: Container = Provide[Container]): service = container.service() ...
🌐
OWASP Foundation
owasp.org › www-community › attacks › xss
Cross Site Scripting (XSS) | OWASP Foundation
Cross Site Scripting (XSS) on the main website for The OWASP Foundation. OWASP is a nonprofit foundation that works to improve the security of software.
🌐
Ets-labs
python-dependency-injector.ets-labs.org › providers › configuration.html
Configuration provider — Dependency Injector 4.49.1 documentation
Configuration provides configuration options to the other providers. This page demonstrates how to use Configuration provider to inject the dependencies, load a configuration from an ini or yaml file, a dictionary, an environment variable, or a pydantic settings object.
🌐
GitHub
github.com › ets-labs › python-dependency-injector › issues › 351
inheritance & dependencies · Issue #351 · ets-labs/python-dependency-injector
January 10, 2021 - Next, Conjoiner is a problem view (Or has a problem view -- though I'd prefer is a problem view so it can inherit "has a problem" -- but either way I can't get it to work). It takes a CutsContext as a dependency.
Author   ets-labs
🌐
Fedora
packages.fedoraproject.org › pkgs › python-injector
python-injector - Fedora Packages
python3-injector - Python dependency injection framework inspired by Guice · python3-injector-doc - Documentation for Python dependency injection framework ·