Cannot import name 'Container' from 'dependency_injector.containers'
See original GitHub issueHey!
I am trying to add type annotations to my Python code wherever I can. I created simple functions to init/shutdown resources in nested containers:
from dependency_injector.containers import Container
from dependency_injector.providers import Container as ContainerProvider
def init_resources(container: Container) -> NoReturn:
container.init_resources()
for provider in container.providers.values():
if isinstance(provider, ContainerProvider):
init_resources(provider)
def shutdown_resources(container: Container) -> NoReturn:
container.shutdown_resources()
for provider in container.providers.values():
if isinstance(provider, ContainerProvider):
shutdown_resources(provider)
I see that containers.pyi
declares Container
class but it seems that containers don’t have Container
parent at all.
I can prepare PR with abstract Container
class.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
cannot import name 'Container' from 'collections' - heroku ...
Container , Iterable , MutableSet , and other abstract base classes are in collections.abc since Python 3.3.
Read more >Wiring — Dependency Injector 4.41.0 documentation
Container will resolve them corresponding to the module of the container class: # In module "yourapp.container": class Container(containers.
Read more >ImportError: cannot import name Container from collections
The Python "ImportError: cannot import name 'Container' from 'collections'" occurs for multiple reasons: Trying to import the Container class ...
Read more >dependency-injector 3.8.0 - PyPI
Logger, name='example') class Gateways(containers.DeclarativeContainer): """IoC container of gateway (API clients to remote services) providers.
Read more >Services and dependency injection in Drupal 8+
A service container (or dependency injection container) is a PHP object ... where example is the name of the module defining the service....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Fixed in
4.16.0
. Thanks for reporting the problem. Closing the issue, re-open if needed.Btw, starting from version
4.14.0
you don’t need to have these recursive functions. When you call.init_resources()
on root container, it will initialize all nested resources. The same for shutdown. If you have other recursive functions, see new feature: https://python-dependency-injector.ets-labs.org/containers/traversal.html