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.

Answer from Serban Teodorescu on Stack Overflow
๐ŸŒ
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.
Discussions

python - What is a Pythonic way for Dependency Injection? - Stack Overflow
Thanks for the link to the 'inject' library. This is the closest I've found so far to filling the gaps that I wanted filled by DI -- and bonus, it's actually being maintained! 2019-09-02T15:46:55.387Z+00:00 ... Save this answer. ... Show activity on this post. Dependency injection is a simple technique that Python ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Python Dependency Injection Framework - Stack Overflow
Copyfrom py3njection import inject ... Demo: @inject def __init__(self, object_to_use: ClassToInject): self.dependency = object_to_use demo = Demo() ... Save this answer. ... Show activity on this post. I recently released a neat (IMHO) micro library for DI in python... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Dependency Injection in Python?
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. More on reddit.com
๐ŸŒ r/Python
32
8
November 24, 2022
Dependency Injection and Python
Dependency Injection is a simple, language agnostic technique for achieving better testability of our code. testability improves, but it also leads to better application organization. I present you with the options I've found to fit the requirements: And I present to you several more, including a link to this discussion. Now off to watch the video you linked to: https://github.com/metaperl/python-oop/wiki/Dependency-Injection--&--Inversion-of-Control More on reddit.com
๐ŸŒ r/Python
6
7
March 21, 2021
๐ŸŒ
PyPI
pypi.org โ€บ project โ€บ dependency-injector
dependency-injector ยท PyPI
The following attestation bundles were made for dependency_injector-4.49.1-cp310-abi3-win32.whl: Publisher: publishing.yml on ets-labs/python-dependency-injector Attestations: Values shown here reflect the state when the release was signed and may no longer be current.
      ยป pip install dependency-injector
    
Published ย  Jun 18, 2026
Version ย  4.49.1
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ python-dependency-injection
Python Dependency Injection: A Guide for Cleaner Code Design | DataCamp
May 7, 2026 - You can implement dependency injection in Python either manually or by using libraries such as dependency_injector.
๐ŸŒ
Wasinski
wasinski.dev โ€บ comparison-of-dependency-injection-libraries-in-python
Comparison of Dependency Injection Libraries in Python, and my favorite one ยท wasinski
December 7, 2020 - We have 3 libraries which match the definition: alecthomas/injector I do not like its interface, but I know itโ€™s being used in production by some people ยท ets-labs/python-dependency-injector very expanded library, with constant support, the problem is itโ€™s boilerplate, if that does not bother you, then itโ€™s a great choice
๐ŸŒ
Medium
medium.com โ€บ @iamsdt โ€บ injectq-the-modern-python-dependency-injection-library-8f6ad6cdb600
InjectQ: The Modern Python Dependency Injection Library | by Shudipto Trafder | Medium
December 3, 2025 - InjectQ โ€” a lightweight, powerful dependency injection library that feels intuitive. It's as simple as a dictionary, yet as capable as enterprise frameworks, designed for contemporary Python development.
๐ŸŒ
GitHub
github.com โ€บ sfermigier โ€บ awesome-dependency-injection-in-python
GitHub - sfermigier/awesome-dependency-injection-in-python: A curated list of awesome things related to dependency inversion / dependency injection in Python. (Contributions welcomed). ยท GitHub
injection โ˜…18 - replacement for python-dependency-injector that works with Python 3.8-3.12 and works with FastAPI, DRF, Flask and Litestar [๐Ÿ, MIT License]. Injex โ˜…14 - Tiny typed dependency injection container with constructor injection, singleton/transient/scoped lifetimes, test overrides, and graph validation before startup. Zero runtime dependencies. [๐Ÿ, MIT License]. Clean IoC โ˜…10 - A simple unintrusive dependency injection library for python with strong support for generics [๐Ÿ, MIT License].
Author ย  sfermigier
Find elsewhere
๐ŸŒ
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 ... easier. Dependency Injector is a lightweight, comprehensive DI tool for Python that simplifies management of application components and their dependencies....
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.

๐ŸŒ
GitHub
github.com โ€บ google โ€บ pinject
GitHub - google/pinject: A pythonic dependency injection library. ยท GitHub
January 10, 2023 - Pinject is a dependency injection library for python.
Starred by 1.3K users
Forked by 86 users
Languages ย  Python 99.5% | Makefile 0.5%
๐ŸŒ
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 - Here are the high-level abstractions for reference. The inject decorator registers dependencies. Module class creates implementations and defines binding. The Application will instantiate and call the Injector to create all objects. Building onto the simple usage from the library docs:
๐ŸŒ
Snyk
snyk.io โ€บ blog โ€บ dependency-injection-python
Dependency injection in Python | Snyk
October 31, 2023 - You should use the Dependency Injector ... injected and managed. The Injector library is a general-purpose DI framework that can be integrated into different Python projects, regardless of the framework....
๐ŸŒ
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.
Top answer
1 of 16
25

Spring Python is an offshoot of the Java-based Spring Framework and Spring Security, targeted for Python. This project currently contains the following features:

  • Inversion Of Control (dependency injection) - use either classic XML, or the python @Object decorator (similar to the Spring JavaConfig subproject) to wire things together. While the @Object format isn't identical to the Guice style (centralized wiring vs. wiring information in each class), it is a valuable way to wire your python app.
  • Aspect-oriented Programming - apply interceptors in a horizontal programming paradigm (instead of vertical OOP inheritance) for things like transactions, security, and caching.
  • DatabaseTemplate - Reading from the database requires a monotonous cycle of opening cursors, reading rows, and closing cursors, along with exception handlers. With this template class, all you need is the SQL query and row-handling function. Spring Python does the rest.
  • Database Transactions - Wrapping multiple database calls with transactions can make your code hard to read. This module provides multiple ways to define transactions without making things complicated.
  • Security - Plugin security interceptors to lock down access to your methods, utilizing both authentication and domain authorization.
  • Remoting - It is easy to convert your local application into a distributed one. If you have already built your client and server pieces using the IoC container, then going from local to distributed is just a configuration change.
  • Samples - to help demonstrate various features of Spring Python, some sample applications have been created:
    • PetClinic - Spring Framework's sample web app has been rebuilt from the ground up using python web containers including: CherryPy. Go check it out for an example of how to use this framework. (NOTE: Other python web frameworks will be added to this list in the future).
    • Spring Wiki - Wikis are powerful ways to store and manage content, so we created a simple one as a demo!
    • Spring Bot - Use Spring Python to build a tiny bot to manage the IRC channel of your open source project.
2 of 16
21

I like this simple and neat framework.

http://pypi.python.org/pypi/injector/

Dependency injection as a formal pattern is less useful in Python than in other languages, primarily due to its support for keyword arguments, the ease with which objects can be mocked, and its dynamic nature.

That said, a framework for assisting in this process can remove a lot of boiler-plate from larger applications. That's where Injector can help. It automatically and transitively provides keyword arguments with their values. As an added benefit, Injector encourages nicely compartmentalized code through the use of Module s.

While being inspired by Guice, it does not slavishly replicate its API. Providing a Pythonic API trumps faithfulness.

๐ŸŒ
Medium
medium.com โ€บ illuin โ€บ dependency-injection-made-easy-in-python-45ece11efeca
Dependency injection made easy in Python | by Thomas Richard | Illuin | Medium
October 21, 2020 - So we decided to write a new dependency injection Python library, opyoid, to be able to easily resolve dependencies between classes and simplify the setup in large applications.
๐ŸŒ
PyPI
pypi.org โ€บ project โ€บ inject
inject ยท PyPI
Python dependency injection framework.
      ยป pip install inject
    
Published ย  Jun 20, 2025
Version ย  5.3.0
๐ŸŒ
GitHub
github.com โ€บ tailhook โ€บ injections
GitHub - tailhook/injections: Simple dependency injection library for python2 and python3 ยท GitHub
Simple dependency injection library for python2 and python3 - tailhook/injections
Starred by 38 users
Forked by 3 users
Languages ย  Python
๐ŸŒ
OneUptime
oneuptime.com โ€บ home โ€บ blog โ€บ how to implement dependency injection in python
How to Implement Dependency Injection in Python
February 3, 2026 - For larger applications, the dependency-injector library provides a container that manages object creation and lifecycle.
๐ŸŒ
Netguru
netguru.com โ€บ home page โ€บ blog โ€บ dependency injection with python, make it easy!
Dependency Injection With Python, Make It Easy!
October 11, 2023 - In all of my projects, I use kink (kodemore/kink) โ€“ it's a library created by my friend. ๐Ÿ˜‰ ยท It provides us with a dependency injection container with an elegant pythonic way of using it, together with just one Python decorator to mark classes and functions that need the injection.