ImportError: cannot import name
I figured it out... the library I was trying to import had the same name as my actual python file. So my python file was called translate.py and my library I was trying to import was called translate. I don't know how to differentiate it.. but changing the name of my python file fixed it. wow.. that took about 3 hours to realize.
Related videos on Youtube
Comments
-
king almost 2 years
I got a library called google-translate-python. https://github.com/terryyin/google-translate-python
Basically, I copied/pasted the
translate.py
file to my python27/lib directory. I imported it like so:from translate import Translator
And I put in something like this:
theTranslate = Translator(to_lang="sp") translation = theTranslate.translate("hello")
And I'm using pycharm btw so I haven't gotten any errors, it is saying the methods are there and everything.
However, I get the error:
ImportError: cannot import name Translator
Did I import the library wrong? that's all I can think of. Because the methods are there and running.
-
SiHa about 8 yearsFrom the github page you linked: Installation:
pip install translate
. -
king about 8 yearsyeah i cant get pip to work at all, i just copied and pasted it, is it not enough?
-
-
king about 8 yearswell i copied it patsed to the python library directory and it is accepting it, it doesnt show a red line or anything. even when i access it, it is finding the class and showing the methods to it, it doesnt make sense why it is giving the error
-
webapp about 8 yearsTry doing the package installation through the Project Interpreter, I think it has to do with visibility to the methods available in that class
-
webapp about 8 yearsGoto Settings>Project Interpreter> There you will see the libraries installed....click the + button which will take you to the list of available libraries and just type Translate...this will show you the package and its description. FYI...if using MAC settings is under Preferences
-
mfitzp about 8 years@simon Do you have a file named "translate.py" in the same folder you are running your script? ie. is one of the files you've created named that.
-
mfitzp about 8 yearsYou can do
import translate; print translate.__file__
to see which file is being imported. -
king about 8 yearsmy library was called the same thing as my python program lol :(
-
king about 8 yearsyes htat was the problem.. i figured it out earlier right before yo usaid that... same file name as library
-
mfitzp about 8 years@simon It happens to us all don't worry ;)
-
webapp about 8 yearsIm glad you figured it out...it happens:)
-
DyingIsFun about 7 yearsI committed the same error with PyDictionary. The python file must be different from the library name!