Cannot use the Translator() function from googletrans module in Python

Question:

I’m using this code to translate the string ‘Hola mundo’:

text = 'Hola mundo'
from googletrans import Translator
Translator().translate(text) 

But I obtain this error:

AttributeError: ‘NoneType’ object has no attribute ‘group’

I’m using Jupyter Notebook with Python version 3.9 and pip version 21.2.4.

Could someone help me with this issue? Thank you in advance!

Asked By: Chanalber

||

Answers:

You have to create an instance of Translator to use this API

from googletrans import Translator
translator = Translator()
translator.translate(text)
Answered By: Abdo Sabry

It works for me using version 4.0.0rc1. You can find a list of all versions here.

from googletrans import Translator


Translator().translate("Hola mundo").text # Output: 'Hello World'

The stable version 3.0.0 returns your error, which is a known bug.

Answered By: Confused Learner

I tried installing the 4.0.0rc1 version for googletrans module and it works!

Thank you 🙂

Answered By: Chanalber