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)
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:
or 2) Update your samples to explicitly use the v2 version of the library if you are using the
2.0.0library version: https://github.com/GoogleCloudPlatform/python-docs-samples/pull/2498Also looks like your documentation on PyPi is out of date:
@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
v2API, like others have suggested:from google.cloud import translate_v2 as translate.Converting calls to
Client.translateto the new API:Clienttofrom google.cloud.translate import TranslationServiceClientClient.translatetoTranslationServiceClient.translate_texttranslateexpected either a string or a list of strings, the newtranslate_texthas 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)source_languagetosource_language_codetarget_languagetotarget_language_codeparentargument. 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 agoogle.oauth2.service_account.Credentialsobject that you made withgoogle.oauth2.service_account.Credentials.from_service_account_filewith a file that has theproject_idparameter set.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 aTranslateTextResponseobject (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 ofTranslationobjects (if you print them, they look like invalid JSON, but again, they aren’t).An example:
I can confirm that this code makes it work
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:
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
FutureWarningsorPendingDeprecationWarningsor 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.