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

Created on 26 Oct 2019  路  14Comments  路  Source: googleapis/google-cloud-python

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',
    },
]
question translation

Most helpful comment

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

All 14 comments

I've had the same problem

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

I am running Python 3.7.3
[GCC 8.3.0] on linux (WSL) via Windows 10 Pro
using a venv

I'm using the demo code from
https://cloud.google.com/translate/docs/quickstart-client-libraries#client-libraries-usage-python

I believe I have all the correct dependencies installed

Requirement already satisfied, skipping upgrade: google-api-core[grpc]<2.0.0dev,>=1.14.0 in ./venv/lib/python3.7/site-packages (from google-cloud-translate) (1.14.3)
Requirement already satisfied, skipping upgrade: google-cloud-core<2.0dev,>=1.0.3 in ./venv/lib/python3.7/site-packages (from google-cloud-translate) (1.0.3)
Requirement already satisfied, skipping upgrade: protobuf>=3.4.0 in ./venv/lib/python3.7/site-packages (from google-api-core[grpc]<2.0.0dev,>=1.14.0->google-cloud-translate) (3.10.0)
Requirement already satisfied, skipping upgrade: setuptools>=34.0.0 in ./venv/lib/python3.7/site-packages (from google-api-core[grpc]<2.0.0dev,>=1.14.0->google-cloud-translate) (40.8.0)
Requirement already satisfied, skipping upgrade: six>=1.10.0 in ./venv/lib/python3.7/site-packages (from google-api-core[grpc]<2.0.0dev,>=1.14.0->google-cloud-translate) (1.12.0)
Requirement already satisfied, skipping upgrade: googleapis-common-protos<2.0dev,>=1.6.0 in ./venv/lib/python3.7/site-packages (from google-api-core[grpc]<2.0.0dev,>=1.14.0->google-cloud-translate) (1.6.0)
Requirement already satisfied, skipping upgrade: pytz in ./venv/lib/python3.7/site-packages (from google-api-core[grpc]<2.0.0dev,>=1.14.0->google-cloud-translate) (2019.3)
Requirement already satisfied, skipping upgrade: requests<3.0.0dev,>=2.18.0 in ./venv/lib/python3.7/site-packages (from google-api-core[grpc]<2.0.0dev,>=1.14.0->google-cloud-translate) (2.22.0)
Requirement already satisfied, skipping upgrade: google-auth<2.0dev,>=0.4.0 in ./venv/lib/python3.7/site-packages (from google-api-core[grpc]<2.0.0dev,>=1.14.0->google-cloud-translate) (1.6.3)
Requirement already satisfied, skipping upgrade: grpcio<2.0dev,>=1.8.2; extra == "grpc" in ./venv/lib/python3.7/site-packages (from google-api-core[grpc]<2.0.0dev,>=1.14.0->google-cloud-translate) (1.24.3)
Requirement already satisfied, skipping upgrade: idna<2.9,>=2.5 in ./venv/lib/python3.7/site-packages (from requests<3.0.0dev,>=2.18.0->google-api-core[grpc]<2.0.0dev,>=1.14.0->google-cloud-translate) (2.8)
Requirement already satisfied, skipping upgrade: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in ./venv/lib/python3.7/site-packages (from requests<3.0.0dev,>=2.18.0->google-api-core[grpc]<2.0.0dev,>=1.14.0->google-cloud-translate) (1.25.6)
Requirement already satisfied, skipping upgrade: certifi>=2017.4.17 in ./venv/lib/python3.7/site-packages (from requests<3.0.0dev,>=2.18.0->google-api-core[grpc]<2.0.0dev,>=1.14.0->google-cloud-translate) (2019.9.11)
Requirement already satisfied, skipping upgrade: chardet<3.1.0,>=3.0.2 in ./venv/lib/python3.7/site-packages (from requests<3.0.0dev,>=2.18.0->google-api-core[grpc]<2.0.0dev,>=1.14.0->google-cloud-translate) (3.0.4)
Requirement already satisfied, skipping upgrade: rsa>=3.1.4 in ./venv/lib/python3.7/site-packages (from google-auth<2.0dev,>=0.4.0->google-api-core[grpc]<2.0.0dev,>=1.14.0->google-cloud-translate) (4.0)
Requirement already satisfied, skipping upgrade: pyasn1-modules>=0.2.1 in ./venv/lib/python3.7/site-packages (from google-auth<2.0dev,>=0.4.0->google-api-core[grpc]<2.0.0dev,>=1.14.0->google-cloud-translate) (0.2.7)
Requirement already satisfied, skipping upgrade: cachetools>=2.0.0 in ./venv/lib/python3.7/site-packages (from google-auth<2.0dev,>=0.4.0->google-api-core[grpc]<2.0.0dev,>=1.14.0->google-cloud-translate) (3.1.1)
Requirement already satisfied, skipping upgrade: pyasn1>=0.1.3 in ./venv/lib/python3.7/site-packages (from rsa>=3.1.4->google-auth<2.0dev,>=0.4.0->google-api-core[grpc]<2.0.0dev,>=1.14.0->google-cloud-translate) (0.4.7)

I am experiencing the exact same thing..
I tried uninstalling and reinstalling it but without luck.

from google.cloud import translate

translate_client = translate.Client()
Traceback (most recent call last):

File "", line 1, in
translate_client = translate.Client()

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

Hi,
the same problem for me either.
I followed the steps here
https://cloud.google.com/translate/docs/reference/libraries#client-libraries-install-python
also installing
pip install --upgrade google-cloud-translate

If I move the same script in a folder where I did the same steps 2-3 months ago, it works instead!
Has anything changed in the libraries?

Python 3.6.8 (default, Oct 7 2019, 12:59:55)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

AttributeError: module 'google.cloud.translate' has no attribute 'Client'
File "", line 1
AttributeError: module 'google.cloud.translate' has no attribute 'Client'

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

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

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

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

Apologies for any confusion caused by the README. I've opened #9536 to fix that and some other issues with the docs. Please reference the v2 and v3 reference documentation at these links below:

As far as deprecation warning:

We released this as a _major_ version bump 1.7.0 -> 2.0.0 to indicate that there were breaking changes. We follow the semantic versioning specification (semver.org). Changes are noted in the CHANGELOG.md and the release notes on GitHub

For GA libraries (versions >=1.0.0) please make sure to (1) pin to a specific version or (2) specify a version range less than the next major version. Otherwise you can end up installing a version of this library that is incompatible with your codebase.

(1) pin to a specific version that you know works with your code

google-cloud-translate==1.7.0

(2) specify a version range less than next major

google-cloud-translate<2.0.0dev

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

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

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!

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

@verhovsky Thanks for posting that migration guide!

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."],

Hi all, a new major version was released recently: #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: GoogleCloudPlatform/python-docs-samples#2498

from google.cloud import translate_v2 as translate

It worked for me

Was this page helpful?
0 / 5 - 0 ratings