Skip to content
Advertisement

Google Cloud Translate API client attribute

I am trying to use the Google Translate API. I’ve installed the required libraries as per their guide and created a virtual environment. However, when running the following sample code which was provided on the website, I keep getting an error regarding the attributes of the library:

"""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']))

Output:

    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'

Why am I getting this error?

Advertisement

Answer

Given that a long time has passed with no answer I’ll summarize the answer given within the comments by Christopher and tested by Omar. Posts should have an accepted answer if they are properly solved to give more visibility to the post so other users experiencing a similar issue can find it faster.

The root cause of the issue AttributeError: module 'google.cloud.translate_v2' has no attribute 'Client' was found within the environment itself. Creating a new, fresh, virtual environment and re-installing the dependencies solved the issue.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement