Python (googletrans) - AttributeError: 'NoneType' object has no attribute 'group'

4.2k Views Asked by At

Trying to detect language code in Python using googletrans. But it generating ERROR (Refer error block for info).

Require solution for the same

Code :

import googletrans
from googletrans import Translator
translator = Translator()
result = translator.translate('Mitä sinä teet')
print(result.src)
print(result.dest)
print(result.origin)
print(result.text)
print(result.pronunciation)

Error:


C:\ProgramData\Anaconda3\lib\site-packages\googletrans\gtoken.py in _update(self)
     60 
     61         # this will be the same as python code after stripping out a reserved word 'var'
---> 62         code = self.RE_TKK.search(r.text).group(1).replace('var ', '')
     63         # unescape special ascii characters such like a \x3d(=)
     64         code = code.encode().decode('unicode-escape')

AttributeError: 'NoneType' object has no attribute 'group'
2

There are 2 best solutions below

1
On BEST ANSWER

This is a known, reported issue in the googletrans library's github page. Take a look there to see the status of the fix and potential work arounds: https://github.com/ssut/py-googletrans/issues.

According to the most recent comment, installing a different version, googletrans==4.0.0-rc1, appears to work, but with caveats:

fire17 commented 16 days ago
normal pip installation failed but uninstalled and reinstalled
googletrans==4.0.0-rc1
then worked :)
tho the object is wierd, i can access the translated text like

a = translator.translate("hi")
translated_text = a.__dict__()["text"]
1
On

It worked post re-installing googletrans

pip install googletrans==4.0.0-rc1