Python 谷歌云翻译API客户端属性
我正在尝试使用谷歌翻译API。我已经按照他们的指南安装了所需的库,并创建了一个虚拟环境。但是,在运行网站上提供的以下示例代码时,我不断收到有关库属性的错误:Python 谷歌云翻译API客户端属性,python,python-3.x,google-cloud-platform,google-translation-api,Python,Python 3.x,Google Cloud Platform,Google Translation Api,我正在尝试使用谷歌翻译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 """ fr
"""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'
从google.cloud导入translate\u v2作为translate
文件“/Users/omar/Desktop/twilio/.venv/lib/python3.7/site-packa
ges/google/cloud/translate_v2/_init__uuuuuu.py”,第18行,在
从pkg_资源导入获取_分发
文件“/Users/omar/Desktop/twilio/.venv/lib/python3.7/site-packa
ges/pkg_resources/_init__.py”,第3191行,中
@_把你叫到一边
文件“/Users/omar/Desktop/twilio/.venv/lib/python3.7/site-packa
ges/pkg_resources/_init__.py”,第3175行,放在一旁
f(*args,**kwargs)
文件“/Users/omar/Desktop/twilio/.venv/lib/python3.7/site packages/pkg_resources/_init__.py”,第3219行,在_initialize_master_working_集中
用于工作组中的距离
文件“/Users/omar/Desktop/twilio/.venv/lib/python3.7/site packages/pkg_resources/_init__.py”,第3219行,在
用于工作组中的距离
文件“/Users/omar/Desktop/twilio/.venv/lib/python3.7/site packages/pkg_resources/_init__.py”,第2726行,在activate中
声明_名称空间(pkg)
文件“/Users/omar/Desktop/twilio/.venv/lib/python3.7/site packages/pkg_resources/_init__.py”,第2252行,位于declare_命名空间中
_句柄(packageName,路径项)
文件“/Users/omar/Desktop/twilio/.venv/lib/python3.7/site packages/pkg_resources/_init__.py”,第2185行,在句柄中
loader.load_模块(packageName)
文件“/Users/omar/Desktop/twilio/google.py”,第7行,在
translate\u client=translate.client()
AttributeError:模块“google.cloud.translate_v2”没有属性“Client”
为什么我会出现这个错误?考虑到很长一段时间没有答案,我将在Christopher的评论中总结答案,并由Omar测试。文章应该有一个可接受的答案,如果他们被正确地解决,以使文章更具可视性,这样其他遇到类似问题的用户可以更快地找到它
问题的根本原因
AttributeError:module'google.cloud.translate_v2'在环境本身中找不到属性'Client'
。创建一个新的、新鲜的虚拟环境并重新安装依赖项解决了这个问题。鉴于很长一段时间没有答案,我将在Christopher的评论中总结答案,并由Omar测试。文章应该有一个可接受的答案,如果他们被正确地解决,以使文章更具可视性,这样其他遇到类似问题的用户可以更快地找到它
问题的根本原因
AttributeError:module'google.cloud.translate_v2'在环境本身中找不到属性'Client'
。创建一个新的、新鲜的虚拟环境并重新安装依赖项解决了问题。库的版本是什么?您能否确认您已从以下位置正确下载了翻译软件包:pip install google cloud translate==2.0.0
证明我设置环境的方式是错误的。我又做了一次,并再次安装了库,它成功了:)库的版本是什么?你能确认你正确地从下载了翻译包吗:,运行以下命令:pip install google cloud translate==2.0.0
证明我设置环境的方式是错误的。我又做了一次,又安装了一次库,它成功了:)