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.
Answer from gregturn on Stack Overflow
🌐
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
Clean IoC ★10 - A simple unintrusive dependency injection library for python with strong support for generics [🐍, MIT License]. Overlay ★6 - A dependency injection framework with pytest-fixture syntax, plus a configuration language for declarative programming [🐍, MIT License].
Starred by 485 users
Forked by 27 users
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.

Discussions

Introducing Wireup: Modern Dependency Injection for Python
Hi r/FastAPI , Wireup is a performant, concise, and easy-to-use dependency injection container for Python and comes with a FastAPI integration. It allows you to register services and configuration which will then be automatically injected by the container when requested. Key features Feature | Description | Dependency Injection | Inject services and configuration using a clean and intuitive syntax. | Autoconfiguration | Automatically inject dependencies based on their types without additional configuration for the most common use cases. | Interfaces / Abstract classes | Define abstract types and have the container automatically inject the implementation. | Factory pattern | Defer instantiation to specialized factories for full control over object creation when necessary. | Singleton/Transient dependencies | Declare dependencies as transient or singletons which tells the container whether to inject a fresh copy or reuse existing instances. | Declarative/Imperative | Configure services through annotations in a fully declarative style or build everything by code for full control over instantiation. Why wireup over existing packages ⁠Fully typed. Both wireup and your service objects. ⁠No patching! Services/factories are not modified and can be easily tested in isolation from the container or their dependencies. Simple but powerful syntax. Straight to the point. No excessive ceremony or boilerplate. ⁠Easy to introduce to an existing projects ⁠It's predictable: No use of *args, or **kwargs. Service declarations are just like regular classes/dataclasses and can be fully linted and type-checked. Why use Wireup in FastAPI Given that FastAPI already supports dependency injection you might wonder why is it worth using. The benefits for Wireup are as follows: More features. Is significantly less boilerplate-y and verbose Is faster. Example showcasing the above Base Service declaration @service # <- these are just decorators and annotated types to collect metadata. @dataclass class A: start: Annotated[int, Inject(param="start")] def a(self) -> int: return self.start @service @dataclass class B: a: A def b(self) -> int: return self.a.a() + 1 @service @dataclass class C: a: A b: B def c(self) -> int: return self.a.a() * self.b.b() Rest of wireup setup # Register application configuration container.params.put("start", 10) # "start" here matches the name being injected. # Initialize fastapi integration. wireup_init_fastapi_integration(app, service_modules=[services]) This is all the additional setup it requires. Services are self-contained and there is no need for Depends(get_service_object) everywhere. Rest of fastapi code # In FastAPI you have to manually build every object. # If you need a singleton service then it also needs to be decorated with lru_cache. # Whereas in wireup that is automatically taken care of. @functools.lru_cache(maxsize=None) def get_start(): return 10 @functools.lru_cache(maxsize=None) def make_a(start: Annotated[int, Depends(get_start)]): return services.A(start=start) @functools.lru_cache(maxsize=None) def make_b(a: Annotated[services.A, Depends(make_a)]): return services.B(a) @functools.lru_cache(maxsize=None) def make_c( a: Annotated[services.A, Depends(make_a)], b: Annotated[services.B, Depends(make_b)]): return services.C(a=a, b=b) Views @app.get("/fastapi") def fastapi( a: Annotated[A, Depends(make_a)], c: Annotated[C, Depends(make_c)]): return {"value": a.a() + c.c()} @app.get("/wireup") def wireup(a: Annotated[A, Inject()], c: Annotated[C, Inject()]): return {"value": a.a() + c.c()} Results after load testing using "hey" with 50,000 requests calling the above endpoints Wireup | FastAPI | Time (seconds, lower is better) | 8.7 | 14.55 | Reqs/second (higher is better) | 5748 | 3436 Looking forward to your thoughts. ⁠GitHub: https://github.com/maldoinc/wireup ⁠Documentation: https://maldoinc.github.io/wireup ⁠Quickstart: https://maldoinc.github.io/wireup/latest/quickstart/ ⁠Demo application: https://github.com/maldoinc/wireup-demo/ FastAPI integration example: https://maldoinc.github.io/wireup/latest/integrations/fastapi/ More on reddit.com
🌐 r/FastAPI
22
39
June 5, 2024
magic-di: Dependency Injector with minimal boilerplate code, built-in support for FastAPI and Celery
I'm thinking to use magic on my upcoming side project, great stuff It's fascinating to see the Python community steadily introduce new libraries aimed at simplifying Dependency Injection. Some recent additions to the scene include FastDepends and Dishka . I'm curious to see how magic-di will stack up against these. It's like a silent battle for the most efficient and user-friendly DI solution More on reddit.com
🌐 r/Python
10
26
April 8, 2024
Wireup 0.5: Modern Dependency Injection for Python
How does it compare to these projects: ? https://github.com/imwithye/flask-inject https://github.com/python-injector/flask_injector https://github.com/ets-labs/python-dependency-injector More on reddit.com
🌐 r/programming
5
10
October 19, 2023
Lagom - dependency injector
Automated dependency injectors are not very pythonic - I've not seen a great argument for using them in python that doesn't shoot down monkey patching for nonexistent reasons. 230 stars is quite a lot for a module that does something the language simply doesn't need. More on reddit.com
🌐 r/Python
16
17
February 9, 2024
🌐
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...)

🌐
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.
🌐
DataCamp
datacamp.com › tutorial › python-dependency-injection
Python Dependency Injection: A Guide for Cleaner Code Design | DataCamp
May 7, 2026 - Bandit: A Static code analyzer for Python focused on security issues. Dependency injection is not just a theoretical pattern; it is used in various production systems to manage complexity, improve flexibility, and streamline testing. In modern web applications, dependency injection plays a crucial role in managing standard services, such as authentication, logging, and database access. Frameworks like FastAPI play an essential role in resolving routes and dependencies.
🌐
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 - But besides that it has many pros: ... A really good choice! My personal winner is https://github.com/proofit404/dependencies which I’m used to cal Injector, because that’s the name of the main class....
🌐
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 - However, in real-world scenarios, the benefits of a dependency injector framework become more apparent and significant. For instance, Singleton is ideal for loading large ML models at startup, ensuring they are instantiated only once, while Factory is perfect for managing web sessions, creating a new instance for each user request to maintain isolation and security. I’ve added my project structure, which reflects how I organize folders in a Python application to ensure clean and efficient imports.
Find elsewhere
🌐
PyPI
pypi.org › project › dependency-injector
dependency-injector · PyPI
Dependency Injector is a dependency injection framework for Python.
      » pip install dependency-injector
    
Published   Jun 18, 2026
Version   4.49.1
🌐
Snyk
snyk.io › blog › dependency-injection-python
Dependency injection in Python | Snyk
October 31, 2023 - You should use the Dependency Injector when you're working on larger projects that demand robust dependency management, inversion of control, and complex scenarios where you need more control over how dependencies are injected and managed. The Injector library is a general-purpose DI framework that can be integrated into different Python projects, regardless of the framework.
🌐
GitHub
github.com › ets-labs › python-dependency-injector
GitHub - ets-labs/python-dependency-injector: Dependency injection framework for Python · GitHub
Visit the docs to know more about the Dependency injection and inversion of control in Python. ... The documentation is available here. ... You need to specify how to assemble and where to inject the dependencies explicitly. The power of the framework is in its simplicity.
Author   ets-labs
🌐
Adriangb
adriangb.com › di › 0.33.2
Python Dependency Injection - DI
In other words, while you could use this as your a standalone dependency injection framework, you may find it to be a bit terse and verbose. There are also much more mature standalone dependency injection frameworks; I would recommend at least looking into python-dependency-injector since it is currently the most popular / widely used of the bunch.
🌐
Medium
medium.com › @snyksec › dependency-injection-in-python-35da876ed7a3
Dependency injection in Python | by Snyk | Medium
November 1, 2023 - Consider using Django Injector when building Django applications that require seamless integration of DI, especially for class-based views, middleware, and management commands. The Depends function in FastAPI is designed to work with Python’s ...
🌐
Adriangb
adriangb.com › di › 0.36.0
di : pythonic dependency injection
In other words, while you could use this as your a standalone dependency injection framework, you may find it to be a bit terse and verbose. There are also much more mature standalone dependency injection frameworks; I would recommend at least looking into python-dependency-injector since it is currently the most popular / widely used of the bunch.
🌐
GitHub
github.com › python-injector › injector
GitHub - python-injector/injector: Python dependency injection framework, inspired by Guice · GitHub
Inversion of Control Containers and the Dependency Injection pattern (an article by Martin Fowler) ... Simplicity - while being inspired by Guice, Injector does not slavishly replicate its API. Providing a Pythonic API trumps faithfulness.
Starred by 1.5K users
Forked by 95 users
Languages   Python
🌐
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.
🌐
PyPI
pypi.org › project › inject
inject · PyPI
Supports Python 3.9+ (v5.*), 3.5-3.8 (v4.*) and Python 2.7–3.5 (v3.*). Supports context managers. ... @inject.autoparams returns a decorator which automatically injects arguments into a function that uses type annotations.
      » pip install inject
    
Published   Jun 20, 2025
Version   5.3.0
🌐
Startup House
startup-house.com › homepage › blog › dependency injection in python
Dependency Injection in Python: Frameworks and Examples
February 15, 2024 - One of the most versatile solutions in this space is the python Dependency Injector framework, which supports both declarative and dynamic containers. Declarative containers allow developers to define dependencies in a structured, readable way, while dynamic containers enable more flexible runtime composition.
🌐
Wolt
careers.wolt.com › en › blog › tech › introducing-magic-di
Introducing magic-di: A framework-agnostic dependency injector for building maintainable Python platforms & applications
February 11, 2025 - Given Python’s versatility and use across backend services, data science, and machine learning, we believe a DI solution must be framework-agnostic. ... Magic DI, a novel dependency injection library developed at Wolt.
🌐
LibHunt
libhunt.com › l › python › topic › dependency-injection
Top 21 Python Dependency Injection Projects | LibHunt
Dependency Injector with minimal boilerplate code, built-in support for FastAPI and Celery, and seamless integration to basically anything. ... This library allows you to easily replace FastAPI dependencies in your tests. Regular mocking techniques do not work due to the inner working of FastAPI. ... Project mention: Show HN: Engin – a modular application framework for Python ...