google-cloud-python: Translation: AttributeError: module 'google.cloud.translate' has no attribute 'Client'

client = translate.Client()
AttributeError: module 'google.cloud.translate' has no attribute 'Client'

i made a virtual env also tried global env but the error is same.i have tried google logging as well but error is same.using this sample code.

from google.cloud import translate
client = translate.Client()
client.get_languages()
[
    {
        'language': 'af',
        'name': 'Afrikaans',
    },
     ...
]
client.detect_language(['Me llamo', 'I am'])
[
    {
        'confidence': 0.25830904,
        'input': 'Me llamo',
        'language': 'es',
    }, {
        'confidence': 0.17112699,
        'input': 'I am',
        'language': 'en',
    },
]

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 15 (4 by maintainers)

Most upvoted comments

Hi all, a new major version was released recently: https://github.com/googleapis/google-cloud-python/pull/9527

To keep your client code working as expected you can do one of 2 things:

  1. Hard set your library version to 1.7.0 in your requirements.txt
google-cloud-translate==1.7.0

or 2) Update your samples to explicitly use the v2 version of the library if you are using the 2.0.0 library version: https://github.com/GoogleCloudPlatform/python-docs-samples/pull/2498

from google.cloud import translate_v2 as translate

Also looks like your documentation on PyPi is out of date:

>>> from google.cloud import translate
>>> client = translate.Client()

@nnegrey Was there a deprecation warning on this? Perhaps there was but I missed it…

Edit: nevermind, migrating to the v3 API is a bad idea. The new API is computer generated Python code, not designed for use by humans. It’s significantly worse, I regret using it. Just change the import statement to use the v2 API, like others have suggested: from google.cloud import translate_v2 as translate.

Converting calls to Client.translate to the new API:

  • Change Client to from google.cloud.translate import TranslationServiceClient
  • Client.translate to TranslationServiceClient.translate_text
    • The old translate expected either a string or a list of strings, the new translate_text has to be a list of strings, if you send a string, it’ll try to translate it character by characted with predictable results. So if you were converting strings one by one, convert it to a list with one element (or restructure your code to build a list of strings before translating)
    • change source_language to source_language_code
    • change target_language to target_language_code
    • add a parent argument. I can’t say I understand what this is, but setting it to "projects/" + the value of "project_id" parameter from my credentials JSON file. This parameter is required for basically every function in the new API. Even if you’re creating the TranslationServiceClient object with in a google.oauth2.service_account.Credentials object that you made with google.oauth2.service_account.Credentials.from_service_account_file with a file that has the project_id parameter set.
    • instead of accessing the result of the translation as translate(params)['translatedText'] you have to access it as [t.translated_text for t in translate_text(params).translations] (if you were translating strings one at a time, you should get just the first element: [t.translated_text for t in translate_text(params).translations][0]). The function itself returns a TranslateTextResponse object (if you print it, it’s repr is an invalid list of what looks like json objects, but it’s not), which is effectively a list of Translation objects (if you print them, they look like invalid JSON, but again, they aren’t).

An example:

from pathlib import Path
import json

from google.cloud.translate import TranslationServiceClient
from google.oauth2 import service_account

GOOGLE_TRANSLATE_CREDENTIALS_FILE = "creds.json"

GOOGLE_TRANSLATE_CREDENTIALS = service_account.Credentials.from_service_account_file(
    GOOGLE_TRANSLATE_CREDENTIALS_FILE
)
GOOGLE_TRANSLATE_PROJECT_ID = "projects/" + GOOGLE_TRANSLATE_CREDENTIALS.project_id

translate_client = TranslationServiceClient(credentials=GOOGLE_TRANSLATE_CREDENTIALS)
supported_languages = {
    l.language_code
    for l in translate_client.get_supported_languages(
        GOOGLE_TRANSLATE_PROJECT_ID
    ).languages
}
result = translate_client.translate_text(
    ["I spent a lot of time on this."],
    target_language_code="ru",
    source_language_code="en",
    parent=GOOGLE_TRANSLATE_PROJECT_ID,
)

print([t.translated_text for t in result.translations][0])

I can confirm that this code makes it work

# Imports the Google Cloud client library
from google.cloud import translate_v2

# Instantiates a client
translate_client = translate_v2.Client()

Might need to update the quick start guide

https://cloud.google.com/translate/docs/quickstart-client-libraries#client-libraries-install-python

I could not have got v3 working without the help from @verhovsky above. Just one thing: I needed contents= for the first parameter:

translate_text( contents=[“I spent less time on this, thanks to @verhovsky.”],

Thanks for the super prompt followup, guys. This indeed solved it for me:

from google.cloud import translate_v2 as translate

For what it’s worth, I get the semantic versioning bit and how a major release should expect to come with breaking changes, but I was surprised there were no FutureWarnings or PendingDeprecationWarnings or anything of the like. As an example, Tensorflow has been emitting warnings for several minor version cycles now leading up to the 2.0.0 release so that developers had time to migrate their code.

In any case, thanks for the very quick responses. We appreciate the work you put into this package!

@alecthegeek, yep! Also updated the docs site this morning.