🌐
DataCamp
datacamp.com › tutorial › python-dependency-injection
Python Dependency Injection: A Guide for Cleaner Code Design | DataCamp
July 24, 2025 - Learn how to implement Python dependency injection to make your code more modular, testable, and maintainable. Explore manual techniques and frameworks.
🌐
Ets-labs
python-dependency-injector.ets-labs.org › introduction › di_in_python.html
Dependency injection and inversion of control in Python — Dependency Injector 4.48.3 documentation
This page describes a usage of the dependency injection and inversion of control in Python. It contains Python examples that show how to implement dependency injection. It demonstrates a usage of the dependency injection framework Dependency Injector, its container, Factory, Singleton and Configuration providers.
🌐
Readthedocs
pymem.readthedocs.io › en › latest › tutorials › inject_python_interpreter.html
Injecting a python interpreter into any process — Pymem alpha documentation
Tutorials » · Injecting a python interpreter into any process · Edit on GitHub · Previous Next · Pymem allow you to inject python.dll into a target process and then map py_run_simple_string with a single call to inject_python_interpreter().
🌐
Better Stack
betterstack.com › community › guides › scaling-python › python-dependency-injection
Dependency Injection in Python | Better Stack Community
April 29, 2025 - Learn how to implement dependency injection in Python, from basic constructor injection to using the `dependency-injector` library for scalable and testable applications.
🌐
Medium
shivama205.medium.com › dependency-injection-python-cb2b5f336dce
Dependency Injection: Python. Overview | by Shivam Aggarwal | Medium
November 22, 2019 - Create a file named email_client.py containing EmailClient class which depends on the config object. Create a new file named email_reader.py which contains the EmailReader class and depends on the EmailClient object.
🌐
StackHawk
stackhawk.com › stackhawk, inc. › vulnerabilities and remediation › preventing command injection in python: a guide to security
Preventing Command Injection in Python: A Guide to Security
January 13, 2025 - An overview of command injection in python with examples and best security practices including tips on how to find & fix this vulnerability.
🌐
Snyk
snyk.io › blog › dependency-injection-python
Dependency injection in Python | Snyk
October 31, 2023 - Think about creating software that's not only good but excellent — easy to adjust, expand, and secure. That's the essence of DI in Python. If you're trying to figure out how to add dependency injection to your work, you've come to the right place. This tutorial will show you how DI works, how to use it, and how it can make your Python projects better.
🌐
Medium
medium.com › @garzia.luke › injector-library-and-exploring-dependency-injection-in-python-4ce10560cd24
Injector Library and Exploring Dependency Injection in Python | by Luke Garzia | Medium
February 8, 2024 - Injector Library and Exploring Dependency Injection in Python This article covers a crash course in Dependency Injection, highlights the injector library, walks through an example from TaskWeaver …
🌐
Ets-labs
python-dependency-injector.ets-labs.org
Dependency Injector — Dependency injection framework for Python — Dependency Injector 4.49.0 documentation
Dependency injection framework for Python by Roman Mogylatov · Introduction · Examples · Tutorials · Providers · Containers · Wiring · Other examples · API Documentation · Feedback · Changelog · Documentation overview · Next: Introduction · ©2024, Roman Mogylatov.
Find elsewhere
🌐
GitHub
github.com › ivankorobkov › python-inject
GitHub - ivankorobkov/python-inject: Python dependency injection
Create instances the way you like and then inject dependencies into them. Other scopes such as a request scope or a session scope are fragile, introduce high coupling, and are difficult to test.
Starred by 755 users
Forked by 85 users
Languages   Python 98.3% | Makefile 1.7% | Python 98.3% | Makefile 1.7%
🌐
BreakInSecurity
axcheron.github.io › code-injection-with-python
Code Injection with Python - BreakInSecurity
December 29, 2017 - How to inject a backdoor into a PE file with Python.
🌐
GitHub
github.com › sethsec › PyCodeInjection
GitHub - sethsec/PyCodeInjection: Automated Python Code Injection Tool · GitHub
root@playground:/opt/PyCodeInjection# python PyCodeInjectionShell.py -h Usage: python PyCodeInjectionShell.py -c command -p param -u URL python PyCodeInjectionShell.py -c command -p param -r request.file Options: -h, --help show this help message and exit -c CMD Enter the OS command you want to run at the command line -i Interactivly enter OS commands until finished -u URL Specify the URL. URLs can use * or -p to set injection point -p PARAMETER Specify injection parameter.
Starred by 87 users
Forked by 23 users
Languages   Python 96.8% | HTML 2.1% | Shell 1.1%
🌐
ArjanCodes
arjancodes.com › blog › python-dependency-injection-best-practices
Best Practices for Python Dependency Injection | ArjanCodes
January 11, 2024 - This form of injection is widely used in modern frameworks and libraries due to its simplicity and effectiveness in enforcing a clear dependency contract. ```python class Database: def query(self, query: str): # execute query pass class Api: def __init__(self, database: Database): self.database = database def get_user(self, user_id: int): return User(*self.database.query(f"SELECT * FROM users WHERE id={user_id}")) class User: def __init__(self, name: str, email: str): self.name = name self.email = email
🌐
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 - To effectively integrate dependency injection using the Dependency Injector framework in a FastAPI application, two critical steps need to be implemented: container.wire(modules=[…]) — It Registers Modules: You’re telling the DI container which Python modules (or files) contain components that should have dependencies injected into them.
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 › tutorials › index.html
Tutorials — Dependency Injector 4.49.0 documentation
This section contains tutorials that show how to apply dependency injection for popular Python frameworks.
🌐
GitHub
github.com › python-injector › injector
GitHub - python-injector/injector: Python dependency injection framework, inspired by Guice · GitHub
Python dependency injection framework, inspired by Guice - python-injector/injector
Starred by 1.5K users
Forked by 98 users
Languages   Python
🌐
DEV Community
dev.to › sxddhxrthx › introduction-to-dependency-injection-in-python-35c
Introduction to Dependency Injection in Python - DEV Community
March 20, 2021 - Since, we have established this fact above that Student has a dependency on School, we'll now establish this dependency. If you watch carefully, after creating the school dependency will have injected it into student.
🌐
PyPI
pypi.org › project › inject
inject · PyPI
Create instances the way you like and then inject dependencies into them. Other scopes such as a request scope or a session scope are fragile, introduce high coupling, and are difficult to test.
      » pip install inject
    
Published   Jun 20, 2025
Version   5.3.0