ImportError: cannot import name

10,416

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.

Share:
10,416

Related videos on Youtube

king
Author by

king

java, php, html, css, vb, asp

Updated on June 04, 2022

Comments

  • king
    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
      SiHa about 8 years
      From the github page you linked: Installation: pip install translate.
    • king
      king about 8 years
      yeah i cant get pip to work at all, i just copied and pasted it, is it not enough?
  • king
    king about 8 years
    well 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
    webapp about 8 years
    Try doing the package installation through the Project Interpreter, I think it has to do with visibility to the methods available in that class
  • webapp
    webapp about 8 years
    Goto 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
    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
    mfitzp about 8 years
    You can do import translate; print translate.__file__ to see which file is being imported.
  • king
    king about 8 years
    my library was called the same thing as my python program lol :(
  • king
    king about 8 years
    yes htat was the problem.. i figured it out earlier right before yo usaid that... same file name as library
  • mfitzp
    mfitzp about 8 years
    @simon It happens to us all don't worry ;)
  • webapp
    webapp about 8 years
    Im glad you figured it out...it happens:)
  • DyingIsFun
    DyingIsFun about 7 years
    I committed the same error with PyDictionary. The python file must be different from the library name!