py-googletrans: AttributeError: 'NoneType' object has no attribute 'group'

Source code:

from googletrans import Translator

# translator = Translator(service_urls=[
# 	'translate.google.com',
# ])
translator = Translator()

data = str(open('first-post.md', encoding='utf8', newline='\n').read())
# print(data)
translations = translator.translate([data], src='en', dest='fa')
print(translations)
print(translations._response.http_version)

for translation in translations:
	print(translation.origin, ' -> ', translation.text)

Output:

C:\Users\Max\Desktop>python translate.py
Traceback (most recent call last):
  File "C:\Users\Max\Desktop\translate.py", line 10, in <module>
    translations = translator.translate([data], src='en', dest='fa')
  File "C:\Users\Max\AppData\Local\Programs\Python\Python310\lib\site-packages\googletrans\client.py", line 177, in translate
    translated = self.translate(item, dest=dest, src=src, **kwargs)
  File "C:\Users\Max\AppData\Local\Programs\Python\Python310\lib\site-packages\googletrans\client.py", line 182, in translate
    data = self._translate(text, dest, src, kwargs)
  File "C:\Users\Max\AppData\Local\Programs\Python\Python310\lib\site-packages\googletrans\client.py", line 78, in _translate
    token = self.token_acquirer.do(text)
  File "C:\Users\Max\AppData\Local\Programs\Python\Python310\lib\site-packages\googletrans\gtoken.py", line 194, in do
    self._update()
  File "C:\Users\Max\AppData\Local\Programs\Python\Python310\lib\site-packages\googletrans\gtoken.py", line 62, in _update
    code = self.RE_TKK.search(r.text).group(1).replace('var ', '')
AttributeError: 'NoneType' object has no attribute 'group'

More information:

C:\Users\Max\Desktop>python --version
Python 3.10.8

Content of init.py file:

"""Free Google Translate API for Python. Translates totally free of charge."""
__all__ = 'Translator',
__version__ = '3.0.0'


from googletrans.client import Translator
from googletrans.constants import LANGCODES, LANGUAGES  # noqa

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 6
  • Comments: 22

Commits related to this issue

Most upvoted comments

Actually, I just got it to fix by updating to the newest version. That will solve the problem

Oh nice, How and to which version?

pip install googletrans==4.0.0-rc1

Actually, I just got it to fix by updating to the newest version. That will solve the problem

Oh nice, How and to which version?

4.0.0-rc1 this version works

For those who have the same problem. Just make sure; don’t forget to install the working versions (4.0.0-rc1 or 3.1.0a0) in the venv, like me. it took me 1 hour to figure out where I went wrong.

Probably helpful

However, this link worked for me. It uses an older version of googletrans pip install googletrans==3.1.0a0

Try pip install googletrans==4.0.0-rc1

I installed it but now it gives the following error.

File "C:\Users\...\Programs\Python\Python310\lib\json\__init__.py", line 339, in loads
    raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not NoneType

I remember that I was able to fix this issue, but I cannot remember exactly how. However It seems that it works on version 4.0.0-rc1, so instead of doing pip install try to force it to do the most recent version. Seems backwards but I believe that is how I fixed it, all I remember was that it was an easier fix than I thought.