1

我在运行 Google Cloud Natural Language API 时遇到了很多麻烦。我正在尝试在 Google VM 上的 Linux 中运行 Python 程序。我也不确定如何运行错误的完整追溯。以下代码不起作用:

from google.cloud import language from google.cloud.language import enums from google.cloud.language import types.

在 Python 2 上出现错误:

Traceback (most recent call last): File "natlangex.py", line 3, in <module> from google.cloud.language import enums ImportError: cannot import name enums

我尝试升级到 Python 3,当我运行 Python 3 时出现错误:我更新到 Python 3。令人困惑的一件事是,当我运行 Python 3 时,错误有点不同:

Traceback (most recent call last): File "natlangex.py", line 2, in <module> from google.cloud import language ImportError: No module named 'google'

以下是我尝试过的其他事情:

  • 我安装了 SDK。
  • 我设置export GOOGLE_APPLICATION_CREDENTIALS="apikey.json"
  • 我试过了!pip install google-cloud-language
  • 我升级了点子pip install --upgrade setuptools pip.

我很失落!先感谢您。

4

1 回答 1

0

由于库更新,您似乎使用了错误的语法,您现在可以参考 [1] 之后的新语法,更具体地说 [2]。

谷歌云的官方自然语言文档似乎尚未更新 [3],但在本文档的末尾,您有指向 Python 客户端库 [1][2] 的链接,其中解释了更改。

你的进口应该是:

from google.cloud import language_v1
from google.cloud.language_v1 import enums
from google.cloud.language_v1 import types

[1] https://googleapis.dev/python/language/latest/api.html

[2] https://googleapis.dev/python/language/latest/gapic/v1/api.html

[3] https://cloud.google.com/natural-language/docs/reference/libraries#client-libraries-usage-python

于 2020-04-20T08:11:51.063 回答