我正在尝试Goslate:免费的Google Translate 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'


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

最佳答案

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

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


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

10-08 14:15