king
king

Reputation: 1344

ImportError: cannot import name

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.

Upvotes: 3

Views: 14432

Answers (4)

king
king

Reputation: 1344

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.

Upvotes: 9

mfitzp
mfitzp

Reputation: 15545

If you can't use pip the simplest way to get this installed would be to do download the source code (.zip file) and unzip it.

Open a terminal (where you have access to python) and change to the folder (cd <the path to the folder>) you have unzipped, and then run:

python setup.py install 

This will make sure the files end up in the right location (which on Windows is actually in C:\Python27\Lib\site-packages).

Upvotes: 0

BChow
BChow

Reputation: 471

Based on the github page the package can be installed from the source using:

python setup.py install

Another option is to save the translate.py to the local directory or another directory.

If translate.py is not in the local directory you can add the module path using:

sys.path.append('PATH_TO_TRANSLATE.PY')

Upvotes: 0

webapp
webapp

Reputation: 708

Does it show in the list of packages installed under Pycharm interpreter? You need to add the package to this list and then it becomes available to you for import. It is available as one of the packages there.

Upvotes: 0

Related Questions