我有一个名为 google-translate-python 的库。 https://github.com/terryyin/google-translate-python
基本上,我将 translate.py
文件复制/粘贴到我的 python27/lib 目录中。我是这样导入的:
from translate import Translator
然后我输入了这样的内容:
theTranslate = Translator(to_lang="sp")
translation = theTranslate.translate("hello")
而且我正在使用 pycharm 顺便说一句,所以我没有收到任何错误,它说方法在那里和一切。
但是,我收到错误:ImportError: cannot import name Translator
我是不是导入库错了?这就是我能想到的。因为方法在那里并且正在运行。
最佳答案
我想通了...我尝试导入的库与我的实际 python 文件同名。所以我的 python 文件叫做 translate.py,我试图导入的库叫做 translate。我不知道如何区分它。但是更改我的 python 文件的名称修复了它。哇.. 花了大约 3 个小时才意识到。
关于python - 导入错误 : cannot import name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35580749/