ImportError: cannot import name defaultdict - python

I'm getting this really strange ImportError when running from collections import defaultdict:
ImportError: cannot import name defaultdict
I'm running python 2.7, and the strange part is that in other parts of my application this exact same import line succeeds.
I thought maybe that's a circular import, but it doesn't make much sense when it comes to built-in python modules.
Any ideas why I get this error?

You probably have a module named 'collections' in your project.
Try renaming this module in your project.

Check that you have your own version of collections.py in python module search path.
It will prevent importing of the standard module collections.
You can confirm that by using following statements:
import collections
print(collections) # => This will print the module information. (esp. path)

most probably you have name clashes.
Try to rename your module or method.
In Python Programming Standarts, Python puts underscore for name clashes you can also do that(http://legacy.python.org/dev/peps/pep-0008/#function-and-method-arguments).

Related

import from lib in python

I import vtt_to_srt converter.I read the instructions.I make the installation.From beginning to this "no problem" but when I import it as manuals describe python can't find the module there.
Installation
pip install vtt_to_srt3
Usage
from vtt_to_srt import vtt_to_srt
path = '/path/to/file.vtt'
vtt_to_srt(path)
Error
ImportError: cannot import name 'vtt_to_srt' from 'vtt_to_srt'
I am a rookie.Sorry for asking this question.
#SakuraFreak's answer was my immediate thought, too, but I ended up investigating this a bit further. It seems like vtt_to_srt stores all its API in the __main__.py file of the module. This file should normally be used when you want to a module using python -m.
This actually makes the module impossible to use the way that the documentation specifies. What I tried, then was:
from vtt_to_srt.__main__ import vtt_to_srt
print(vtt_to_srt)
This results in:
<function vtt_to_srt at 0x000002A0948216A8>
So it seems like this workaround is OK.
I do not know if storing all the module's code in __main__.py is some convention not supported by my version of Python (CPython 3.7.3 on Windows), or if it simply an error. Maybe you should approach the module's owners with this.
Because you're trying to import a function from the module which doesn't exist.
the correct way for this module is:
import vtt_to_srt

NameError: name 'reload' is not defined python 3.6.4

After installing Xadmin, I encounter some problems. These are my error details:
[File "C:\Users\Harry\PycharmProjects\mxonline\lib\site-packages\xadmin\sites.py", line 9, in <module>
reload(sys)
NameError: name 'reload' is not defined][1]
I've tried to import importlib importlib.reload(sys) but it still doesn't work. I am using python 3.6.4.
Assuming that I understand the problem, you are having issues with importing reload from the package importlib and you encounter the NameError when trying to use reload.
If this is all correct, then make sure you are importing reload correctly. If you just want reload try:
from importlib import reload
This will import reload while making it available under the name reload. If you want to give it an alias you could do:
from importlib import reload as foo
Finally, if you needed all of importlib you could also import the package as an alias:
import importlib as il
And then use reload from this like so:
il.reload(sys)
This code is doing something super duper weird and incompatible with Python 3. Importing reload from importlib will not help, even if you were to edit the library's code to import reload, because it is also relying on sys.setdefaultencoding, which does not exist on Python 3.
To use this code on Python 3, you would have to install an updated version directly from github, since the most recent release doesn't have the fix. I don't know whether the code has other issues with Python 3, though. Personally, I would probably not use xadmin at all.

Naming module same as library

I have a folder named 'http' in my project but then the code inside the folder uses another library that uses the http library which can't be resolve because it resolves to my 'http' folder. I tried to have my __init__.py do this:
from __future__ import absolute_import
import http
But it doesn't seem to help. I am using Python3.6
I think it's not a good idea to name your module as already existing standard module. Because it makes you unable to import both within a script in your project. PEP8 recommends to append already existing standard names with underscore symbol in order to resolve such collisions. So I would recommend you to rename your module into http_.
http -> http_
PEP8 - names to avoid

Import package from file with same name as package [duplicate]

I have a module that conflicts with a built-in module. For example, a myapp.email module defined in myapp/email.py.
I can reference myapp.email anywhere in my code without issue. However, I need to reference the built-in email module from my email module.
# myapp/email.py
from email import message_from_string
It only finds itself, and therefore raises an ImportError, since myapp.email doesn't have a message_from_string method. import email causes the same issue when I try email.message_from_string.
Is there any native support to do this in Python, or am I stuck with renaming my "email" module to something more specific?
You will want to read about Absolute and Relative Imports which addresses this very problem. Use:
from __future__ import absolute_import
Using that, any unadorned package name will always refer to the top level package. You will then need to use relative imports (from .email import ...) to access your own package.
NOTE: The above from ... line needs to be put into any 2.x Python .py files above the import ... lines you're using. In Python 3.x this is the default behavior and so is no longer needed.

Python error while using MysqlDb - sets module is deprecated

I'm currently getting the warning every time I run a Python script that uses MySQLdb:
/var/lib/python-support/python2.6/MySQLdb/__init__.py:34:
DeprecationWarning: the sets module is deprecated
from sets import ImmutableSet
I'd rather not mess with their lib if possible. I'm on Ubuntu server. Anyone know an easy way to fix that warning message?
Thanks
UPDATE:
Fixed it based on the suggestions below and this link: https://bugzilla.redhat.com/show_bug.cgi?id=505611
import warnings
warnings.filterwarnings('ignore', '.*the sets module is deprecated.*',
DeprecationWarning, 'MySQLdb')
import MySQLdb
Do this before the mysql module is imported
import warnings
warnings.filterwarnings(action="ignore", message='the sets module is deprecated')
import sets
You can ignore the warning using the warnings module, or the -W argument to Python. Don't ignore all DeprecationWarnings, though, just the ones from MySQLdb :)
All it means is the sets module (more specifically the immutableset part) is deprecated, and you should use it's replacement, set. Set is inbuilt so no need to import.
If you need an immutable set, frozenset() should work.

Categories