【问题标题】:Google Cloud Translate API client attributeGoogle Cloud Translate API 客户端属性
【发布时间】:2020-04-14 08:08:57
【问题描述】:

我正在尝试使用 Google 翻译 API。我已经按照他们的指南安装了所需的库并创建了一个虚拟环境。但是,在运行网站上提供的以下示例代码时,我不断收到有关库属性的错误:

"""Translates text into the target language.

Target must be an ISO 639-1 language code.
See https://g.co/cloud/translate/v2/translate-reference#supported_languages
"""
from google.cloud import translate_v2 as translate
translate_client = translate.Client()

if isinstance(text, six.binary_type):
    text = text.decode('utf-8')

# Text can also be a sequence of strings, in which case this method
# will return a sequence of results for each text.
result = translate_client.translate(
    text, target_language=target)

print(u'Text: {}'.format(result['input']))
print(u'Translation: {}'.format(result['translatedText']))
print(u'Detected source language: {}'.format(
    result['detectedSourceLanguage']))

输出:

    from google.cloud import translate_v2 as translate
  File "/Users/omar/Desktop/twilio/.venv/lib/python3.7/site-packa
ges/google/cloud/translate_v2/__init__.py", line 18, in <module>
    from pkg_resources import get_distribution
  File "/Users/omar/Desktop/twilio/.venv/lib/python3.7/site-packa
ges/pkg_resources/__init__.py", line 3191, in <module>
    @_call_aside
  File "/Users/omar/Desktop/twilio/.venv/lib/python3.7/site-packa
ges/pkg_resources/__init__.py", line 3175, in _call_aside
    f(*args, **kwargs)
  File "/Users/omar/Desktop/twilio/.venv/lib/python3.7/site-packages/pkg_resources/__init__.py", line 3219, in _initialize_master_working_set
    for dist in working_set
  File "/Users/omar/Desktop/twilio/.venv/lib/python3.7/site-packages/pkg_resources/__init__.py", line 3219, in <genexpr>
    for dist in working_set
  File "/Users/omar/Desktop/twilio/.venv/lib/python3.7/site-packages/pkg_resources/__init__.py", line 2726, in activate
    declare_namespace(pkg)
  File "/Users/omar/Desktop/twilio/.venv/lib/python3.7/site-packages/pkg_resources/__init__.py", line 2252, in declare_namespace
    _handle_ns(packageName, path_item)
  File "/Users/omar/Desktop/twilio/.venv/lib/python3.7/site-packages/pkg_resources/__init__.py", line 2185, in _handle_ns
    loader.load_module(packageName)
  File "/Users/omar/Desktop/twilio/google.py", line 7, in <module>
    translate_client = translate.Client()
AttributeError: module 'google.cloud.translate_v2' has no attribute 'Client'

为什么会出现这个错误?

【问题讨论】:

  • 库版本是多少?
  • 您能否确认您从cloud.google.com/translate/docs/reference/libraries/v2/… 正确下载了翻译包,运行以下命令:
  • pip install google-cloud-translate==2.0.0
  • 原来我设置环境的方式是错误的。我又做了一次并再次安装了库,它工作了:)

标签: python python-3.x google-cloud-platform google-translation-api


【解决方案1】:

鉴于已经过去很长时间没有答案,我将总结 Christopher 在 cmets 中给出并由 Omar 测试的答案。如果帖子得到正确解决,则帖子应该有一个可接受的答案,以提高帖子的可见性,以便遇到类似问题的其他用户可以更快地找到它。

问题AttributeError: module 'google.cloud.translate_v2' has no attribute 'Client' 的根本原因是在环境本身中找到的。创建一个全新的虚拟环境并重新安装依赖项解决了这个问题。

【讨论】:

    【解决方案2】:

    这个问题可能有解决方案here——我的意思是pip install --upgrade google-cloud-translate。它对我有用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-26
      • 2017-04-30
      • 2020-02-15
      • 1970-01-01
      • 2019-02-15
      相关资源
      最近更新 更多