ImportError: unable to import defaultdict name

I get this really weird ImportError when starting from collections import defaultdict :

 ImportError: cannot import name defaultdict 

I am running python 2.7, and the strange part is that in other parts of my application this exact same import line succeeds.

I thought this was circular import, but that doesn't really matter when it comes to python built-in modules.

Any ideas why I get this error?

+5
source share
3 answers

You probably have a module named "collections" in your project.

Try renaming this module to your project.

+13
source

Make sure you have your own version of collections.py in the python module search path.

This will prevent the import of the standard collections module.

You can confirm this using the following statements:

 import collections print(collections) # => This will print the module information. (esp. path) 
+2
source

Most likely, you are faced with names.
Try renaming your module or method.
In Python Programming Standart, Python places an underscore for name conflicts, you can also do this ( http://legacy.python.org/dev/peps/pep-0008/#function-and-method-arguments ).

0
source

Source: https://habr.com/ru/post/1200229/


All Articles