'TranslationServiceClient' object has no attribute 'location_path' on Google Cloud Translate "translate_v3"
Asked Answered
M

3

6

I'm using Python 3.6 and google-cloud-translate package.

from google.cloud import translate_v3 as translate
client = translate.TranslationServiceClient(credentials = credentials)
parent = client.location_path("my-location", "global")

Since yesterday, having updated the libraries I get this error:

AttributeError: 'TranslationServiceClient' object has no attribute 'location_path'

Is it that these libraries have changed? What is the right way to channel this inquiry?

Monosymmetric answered 7/8, 2020 at 14:38 Comment(0)
T
7

Also encountered this. Not all the documentation has been updated yet, but they have published a migration guide:

https://googleapis.dev/python/translation/latest/UPGRADING.html

You could replace parent with "projects/<PROJECT_ID>/locations/<LOCATION>"

or define

def location_path(project_id, location):
    # might as well use an f-string, the new library supports python >=3.6
    return f"projects/{project_id}/locations/{location}"

and change client.location_path to location_path if this is something you use in many locations.

There are more sweeping changes, too. They now prefer you to pass a dictionary called request to the API methods, although the old way is still accepted. Thus, your code might look like this:

from google.cloud import translate_v3 as translate

client = translate.TranslationServiceClient(credentials=credentials)

response = client.translate_text(
    request={
        "parent": "projects/my-location/locations/global",
        "target_language_code": target_language_code,
        "contents": [text],
    }
)

Now, you might well ask 'how will I know what to put in that request dictionary?'. It looks as though the library comes with type annotations for the dictionaries appropriate for each method: https://googleapis.dev/python/translation/latest/translate_v3/types.html

For example, I read in your comment on another answer that you have had trouble with the detect_language method. The method signature indicates that if you use keyword arguments, content should be a valid one, so I don't know why that fails - maybe it's a bug.

However, if instead you use a request dictionary, that should look like this. You'll see that the keys don't appear to correspond exactly to the method signature keywords (although content is one of them).

This code would work:

response = client.detect_language({
    "parent": "projects/my-location/locations/global",
    "content": "Tá Gaeilge Agam, ach tá mé i mo chonai i Sasana",
})

lang = response.languages[0].language_code

(the return type is somewhat convoluted, as you can see)

Tiber answered 14/8, 2020 at 10:39 Comment(0)
A
0

I was having the same issue. I got rid of the 'parent' code all together and added it inside of the translate_text method.

client = translate.TranslationServiceClient()

response = client.translate_text(
              parent = 'projects/{}'.format(project_id),
              contents = [text],
              mime_type = 'text/plain',
              source_language_code = 'en-US',
              target_language_code = 'es')

Documentation: https://googleapis.dev/python/translation/latest/translate_v3/services.html

Arctic answered 7/8, 2020 at 17:49 Comment(1)
It seems to work but - still getting more errors from this new release. Have you been able to solve this other error: TypeError: detect_language() got an unexpected keyword argument 'content'Monosymmetric
R
0

There nothing going to work out if python version is 3.6 or 3.8 I have fixed this easily by upgrading to python3.10 then upgrade the other google packages

    google-api-core==2.7.1
    google-auth==2.6.4
    google-auth-oauthlib==0.4.6
    google-cloud-core==2.3.0
    google-cloud-translate==3.7.2
    google-cloud-vision==2.7.2
    google-pasta==0.2.0
    googleapis-common-protos==1.56.0
    grpcio==1.44.0
    grpcio-status==1.44.0

Rheumatoid answered 9/5, 2022 at 13:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.