Python AttributeError: 'module' object has no attribute 'Goslate'

Python AttributeError: 'module' object has no attribute 'Goslate'

我正在尝试 Goslate:免费 Google 翻译 API。我使用 sudo pip install goslate

安装了 goslate

我编写了一个简单的程序,并在我的终端中使用 python getbn.py 命令执行了它。

这里是里面的代码getbn.py:

import goslate
gs = goslate.Goslate()
print(gs.translate('hello world', 'bn'))

执行命令python getbn.py后出现以下错误:

Traceback (most recent call last):
  File "getbn.py", line 1, in <module>
    import goslate
  File "/home/ubuntu/Desktop/goslate.py", line 2, in <module>
    # -*- coding: utf-8 -*-
AttributeError: 'module' object has no attribute 'Goslate'

然后我尝试通过执行python3 getbn.py命令运行程序,发现如下错误:

Traceback (most recent call last):
  File "getbn.py", line 1, in <module>
    import goslate
ImportError: bad magic number in 'goslate': b'\x03\xf3\r\n'

我该如何解决?为什么会有两种不同类型的错误?

您的 Desktop 文件夹中有一个名为 goslate.py 的文件

File "/home/ubuntu/Desktop/goslate.py", line 2, in <module>
      ^^^^^^^^^^^^^^^^^^^^

这不是您使用 pip 安装的模块,它妨碍了导入。

您可能已经简单安装了 goslate,但您可能正在使用不同的虚拟环境。尝试切换到该虚拟环境,然后再次 运行。