question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot import name 'Container' from 'dependency_injector.containers'

See original GitHub issue

Hey!

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:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
rmk135commented, Feb 3, 2021

Fixed in 4.16.0. Thanks for reporting the problem. Closing the issue, re-open if needed.

1reaction
rmk135commented, Feb 2, 2021

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found