ssut / py-googletrans

(unofficial) Googletrans: Free and Unlimited Google translate API for Python. Translates totally free of charge.
http://py-googletrans.rtfd.io
MIT License
3.93k stars 724 forks source link

error in result (AttributeError: 'NoneType' object has no attribute 'group') #234

Open Mohammadwh opened 4 years ago

Mohammadwh commented 4 years ago

hi i use Python 3.6.9 and when i call translate have this error: image

abelcheung commented 3 years ago

The error @perymerdeka has posted always occur when attempting to perform bulk translation (trying to translate a list of strings).

SivaAndMe commented 3 years ago

This worked for me.

pip install googletrans==3.1.0a0

from googletrans import Translator
translator = Translator(service_urls=['translate.googleapis.com','translate.google.com','translate.google.co.kr'])
lang = translator.detect("who are you ?")
result = translator.translate("who are you ?", src='en', dest='hi')
print(lang)
print(result.text)

#Output - 
Detected(lang=en, confidence=1.0)
तुम कौन हो ?

https://stackoverflow.com/a/65109346/9955390 https://github.com/ssut/py-googletrans/issues/234#issuecomment-748245530

perymerdeka commented 3 years ago

The error @perymerdeka has posted always occur when attempting to perform bulk translation (trying to translate a list of strings).

in my case i translate string 10253 characters long in one request, its fail,but I try to split the string, it works, but sometimes the same error appears again, namely typeError

JSMboli commented 3 years ago

This worked for me.

pip install googletrans==3.1.0a0

from googletrans import Translator
translator = Translator(service_urls=['translate.googleapis.com','translate.google.com','translate.google.co.kr'])
lang = translator.detect("who are you ?")
result = translator.translate("who are you ?", src='en', dest='hi')
print(lang)
print(result.text)

#Output - 
Detected(lang=en, confidence=1.0)
तुम कौन हो ?

https://stackoverflow.com/a/65109346/9955390 #234 (comment)

many thannks. This works for me perfectly after suffering for a while.

from googletrans import Translator

translator = Translator() translated = translator.translate('Ina kake ne?. ko zaka bani abinci ne in ci?')

print(translated.text)

Where are you? Will you give me food to eat?

Saatvik-droid commented 3 years ago

pip install googletrans==4.0.0-rc1

This worked for me.

Saravananslb commented 3 years ago

Use this for google translation and it is working fine https://pypi.org/project/pygoogletranslation

Thank me later

Saravananslb commented 3 years ago

Hi all, we find alternate library for googletrans, that is pygoogletranslation and its functionallity is same as the googletrans.

pip install by - pip install pygoogletranslation

For furthur information refer - https://pypi.org/project/pygoogletranslation/
                                https://saravananslb.github.io/py-googletranslation/
                                https://github.com/Saravananslb/py-googletranslation
perymerdeka commented 3 years ago

Hi all, we find alternate library for googletrans, that is pygoogletranslation and its functionallity is same as the googletrans.

pip install by - pip install pygoogletranslation

For furthur information refer - https://pypi.org/project/pygoogletranslation/
                              https://saravananslb.github.io/py-googletranslation/
                              https://github.com/Saravananslb/py-googletranslation

thank you

JSMboli commented 3 years ago

For me, googletrans still works fine, can someone compare the two to tell us what is the difference and which one is better? Julius

abelcheung commented 3 years ago

Just a brief glance: the one @Saravananslb published uses requests as underlying web session library, while the original one we are using utilizes httpx. The core token generation seems to be very similar, if not identical. I haven't checked clearly though, so take this comment with a grain of salt.

Saravananslb commented 3 years ago

Just a brief glance: the one @Saravananslb published uses requests as underlying web session library, while the original one we are using utilizes httpx. The core token generation seems to be very similar, if not identical. I haven't checked clearly though, so take this comment with a grain of salt.

Yes, both are similar but googletrans support max 5000 character but pygoogletranslation support unlimited character.

Token generation process is similar but getting secret key is some what different for these two packages.

Pygoogletranslation support translation from file such as .doc, .docx, .pdf, .txt.

We are keep on updating our package to give end user a very flawless and good experience. We need to your support to improve our package and feedback are welcome.

For more information, please check our pygoogletranslation official python page or GitHub page.

Thanks, Saravananslb

jonas-nothnagel commented 3 years ago

Hi @ssut pip install googletrans==4.0.0-rc1 Is still not working for me and gives the same old error:

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

There have been many proposed workarounds in this thread, but is there a official solution to this problem now?

Saravananslb commented 3 years ago

Hi @ssut pip install googletrans==4.0.0-rc1 Is still not working for me and gives the same old error:

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

There have been many proposed workarounds in this thread, but is there a official solution to this problem now? Yes, pip install by - pip install pygoogletranslation

For moreinformation refer - https://pypi.org/project/pygoogletranslation/ https://saravananslb.github.io/py-googletranslation/ https://github.com/Saravananslb/py-googletranslation

jonas-nothnagel commented 3 years ago

Hi @ssut pip install googletrans==4.0.0-rc1 Is still not working for me and gives the same old error: AttributeError: 'NoneType' object has no attribute 'group' There have been many proposed workarounds in this thread, but is there a official solution to this problem now? Yes, pip install by - pip install pygoogletranslation

For moreinformation refer - https://pypi.org/project/pygoogletranslation/ https://saravananslb.github.io/py-googletranslation/ https://github.com/Saravananslb/py-googletranslation

the pygoogletranslation library also throws several errors. I created an issue on their (your?) github. Thank you for referring. Let's try to keep this thread clean and focused on a solution for py-googletrans :)

Saravananslb commented 3 years ago

Hi @ssut pip install googletrans==4.0.0-rc1 Is still not working for me and gives the same old error: AttributeError: 'NoneType' object has no attribute 'group' There have been many proposed workarounds in this thread, but is there a official solution to this problem now? Yes, pip install by - pip install pygoogletranslation

For moreinformation refer - https://pypi.org/project/pygoogletranslation/ https://saravananslb.github.io/py-googletranslation/ https://github.com/Saravananslb/py-googletranslation

the pygoogletranslation library also throws several errors. I created an issue on their (your?) github. Thank you for referring. Let's try to keep this thread clean and focused on a solution for py-googletrans :)

Yes, sorry for the inconvenience caused. We have updated our package to v2.0.3. Please try this version, I hope we have sorted out all the errors. Let me know your feedback to improve our package. We have unlimited character support in the translation.

fire17 commented 3 years 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"]
LukaZaja commented 3 years ago

So when I use googletrans==4.0.0-rc1 it doesn't error out however there is some weirdness:

>>>translator.detect('sentence written in english.')
<googletrans.models.Detected object at 0x7f8254228700>
>>>print(translator.detect('sentence written in english.'))
Detected(lang=en, confidence=None)

So the repr is not what is supposed to be per documentation and I think the issue is that it's not even defined in Detected class definition, fix for that would be to rename dunder str into repr, str just uses repr as a fallback so nothing else should change.

As for confidence, I tried using Translator.detect_legacy and for some reason it gives me confidence as expected.

>>>print(translator.detect_legacy('sentence written in english.'))
Detected(lang=en, confidence=1)
ziedmaaloul commented 3 years ago

hello , same problem for me :(

ziedmaaloul commented 3 years ago

Hello :

Traceback (most recent call last): File "/usr/local/bin/translate", line 40, in <module> main() File "/usr/local/bin/translate", line 21, in main result = translator.detect(args.text) File "/Library/Python/2.7/site-packages/googletrans/client.py", line 249, in detect data = self._translate(text, dest='en', src='auto') File "/Library/Python/2.7/site-packages/googletrans/client.py", line 75, in _translate token = self.token_acquirer.do(text) File "/Library/Python/2.7/site-packages/googletrans/gtoken.py", line 186, in do self._update() File "/Library/Python/2.7/site-packages/googletrans/gtoken.py", line 65, in _update code = unicode(self.RE_TKK.search(r.text).group(1)).replace('var ', '') AttributeError: 'NoneType' object has no attribute 'group'

Idea ? ;)

ghost commented 3 years ago

@SivaAndMe that work for me also https://github.com/SivaAndMe

SuperZombi commented 3 years ago

I am getting this error too. Fix it please

terryyz commented 3 years ago

I am getting this error too. Fix it please

Check #280

gleidsonh commented 3 years ago

pip install googletrans==4.0.0-rc1

This worked for me.

It worked for me on detecting language. Thanks =]

VictorMegir commented 3 years ago

@ssut pip install googletrans==4.0.0-rc1 worked perfectly for me. I'm using Python 3.9.1. mega thx ❤️ 💯

vesper8 commented 3 years ago

Still getting the JSON object must be str, bytes or bytearray, not NoneType when passing a string greater than 5k and I have uninstalled / reinstalling googletrans==4.0.0rc1

Is this normal?? Has this specific issue been addressed in rc1?

This problem only occurs with googletrans==4.0.0rc1

It works fine on googletrans==3.1.0a0

shivito1 commented 3 years ago

I have tried googletrans==4.0.0-rc1 and googletrans==3.1.0a0 example: x = trans.translate("hello",dest="am") Error:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\UsersuserPycharmProjects\AndroidXMLTranslate\venv\lib\site-packages\googletrans-2.3.0-py3.8.egg\googletrans\client.py", line 172, in translate
    data = self._translate(text, dest, src)
  File "C:\UsersuserPycharmProjects\AndroidXMLTranslate\venv\lib\site-packages\googletrans-2.3.0-py3.8.egg\googletrans\client.py", line 75, in _translate
    token = self.token_acquirer.do(text)
  File "C:\UsersuserPycharmProjects\AndroidXMLTranslate\venv\lib\site-packages\googletrans-2.3.0-py3.8.egg\googletrans\gtoken.py", line 186, in do
    self._update()
  File "C:\UsersuserPycharmProjects\AndroidXMLTranslate\venv\lib\site-packages\googletrans-2.3.0-py3.8.egg\googletrans\gtoken.py", line 65, in _update
    code = unicode(self.RE_TKK.search(r.text).group(1)).replace('var ', '')
AttributeError: 'NoneType' object has no attribute 'group'
iamrulai commented 3 years ago

googletrans == 4.0.0-rc1 is OK to me.

Poycodon9x commented 3 years ago

pip install google_trans_new I have removed googletrans and replaced the state Cai now. It's working again

SuperZombi commented 3 years ago

pip install googletrans==4.0.0rc1 This is pre-release. It's working for me. Author, publish this pre-release as main.

igormcsouza commented 3 years ago

Using Python 3.7 and googletrans==4.0.0rc1 worked like a charm for me! :D

danpaldev commented 3 years ago

This is still an issue. Use pip install googletrans==4.0.0rc1 and you will have no problems.

ghost commented 3 years ago

the issue is still present for bulk translations

liliana3186 commented 3 years ago

I have the same issue Using python 3.7.6 and googletrans==4.0.0rc1

aprknight commented 3 years ago

googletrans 3.0.0 which, at the time of writing, is the released version with Python 3.9.6, macOS 11.5, ISP is BT in the United Kingdom and the 'NoneType object has no attribute group...' issue is a problem for me.

It is obvious from the error trace that the problem lies in: RE_TKK.search(r.text) which is returning None.

Under what circumstances would it do that?

ValleSell commented 2 years ago

This problem occurs if you create a Translator instance for each use of the request. This problem can be partially avoided by using only one instance of the class.

Could you explain how to do that with an example please?

memory4963 commented 2 years ago

I met this issue also, and I fixed it by removing the param "service_urls" when constructing the "Translator()"

from

translator = Translator(service_urls=[
       'translate.google.com',
       'translate.google.com.hk',
       'translate.google.co.kr',
     ])

to

translator = Translator()

hope this helps :)

Avazart commented 2 years ago

File "C:\Users\xxx\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'

Author used regex for parse html :(

DarkRedman commented 2 years ago

This problem occurs if you create a Translator instance for each use of the request. This problem can be partially avoided by using only one instance of the class.

Doesn't work for me even using only one instance.

MiKatre commented 2 years ago

What worked for me today was

pip uninstall googletrans==4.0.0-rc1

pip install googletrans==3.1.0a0

(solution from https://stackoverflow.com/a/69271089/7292383)

DSPerson commented 2 years ago
  1. pip install googletrans==3.1.0a0
  2. translator = Translator()
  3. don't use Translator() service_urls parameter, it work
lgbongiolo commented 2 years ago

best solution here: https://towardsdatascience.com/language-translation-using-python-bd8020772ccc

alessandrokr commented 2 years ago

I fixed it with with changing the the service_urls with the non web version: service_urls = ["translate.googleapis.com"]

frannylac commented 1 year ago

translate.googleapis.com

this url was removed... returns 404

DoubleStarK commented 7 months ago

Python 3.12.2 (main, Feb 6 2024, 20:19:44) [Clang 15.0.0 (clang-1500.1.0.2.5)] on darwin using googletrans==4.0.0rc1, works fine.

vasamaximov commented 6 months ago

googletrans==4.0.0rc1, pythonwin 3.11.4 - works perfectly too