Googletrans Stopped Working with Error 'Nonetype' Object Has No Attribute 'Group'

googletrans stopped working with error 'NoneType' object has no attribute 'group'

Update 01/12/2020: This issue re-emerged lately, (apparently) caused once again by some changes on the Google translation API.

A solution is being discussed (again) in this Github issue. Although there is not a definitive solution yet a Pull Request seem to be solving the problem: https://github.com/ssut/py-googletrans/pull/237.

While we wait for it to be approved it can be installed like this:

$ pip uninstall googletrans
$ git clone https://github.com/alainrouillon/py-googletrans.git
$ cd ./py-googletrans
$ git checkout origin/feature/enhance-use-of-direct-api
$ python setup.py install

Original Answer:

Apparently it's a recent and widespread problem on Google's side.
Quoting various Github discussions, it happens when Google sends you directly the raw token.

It's being discussed right now and there is already a pull request to fix it, so it should be resolved in the next few days.

For reference, see:

https://github.com/ssut/py-googletrans/issues/48 <-- exact same problem reported on the Github repo
https://github.com/pndurette/gTTS/issues/60 <-- seemingly same problem on a text-to-speech library
https://github.com/ssut/py-googletrans/pull/78 <-- pull request to fix the issue

To apply this patch (without waiting for the pull request to be accepted) simply install the library from the forked repo https://github.com/BoseCorp/py-googletrans.git (uninstall the official library first):

$ pip uninstall googletrans
$ git clone https://github.com/BoseCorp/py-googletrans.git
$ cd ./py-googletrans
$ python setup.py install

You can clone it anywhere on your system and install it globally or while inside a virtualenv.

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

As I have tested your scenario using your code, I also encountered the same error as seen in this screenshot.
Testing using googletrans 3.0.0

This is likely due to the old library version that you're using which is googletrans 3.0.0

To resolve the issue you are encountering, you must upgrade your googletrans from 3.0.0 to 4.0.0rc1. You may use below script to upgrade your googletrans version.

pip install googletrans==4.0.0rc1

Please see below successful testing using googletrans 4.0.0rc1 for your reference:
Testing using googletrans 4.0.0rc1

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

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"]

NoneType' object has no attribute 'group' when trying to translate with the googletrans lib for a discord bot

Ok so using this (the fixed lib) solved my problem: https://pypi.org/project/google-trans-new/

There's also an released alpha about the official googletrans lib that has this bug patched, you can find more about it in this thread: googletrans stopped working with error 'NoneType' object has no attribute 'group'

New code:

import discord, google_trans_new, os, dotenv
from discord.ext import commands
from dotenv import load_dotenv
from google_trans_new import google_translator

load_dotenv()
translate = google_translator()
prefix = 'tb!'
bot = commands.Bot(command_prefix=prefix)
bot.remove_command("help")
client = discord.Client()
TOKEN = os.getenv('TOKEN')
translationch = 822024643468460042

@bot.event
async def on_ready():
print(f'{bot.user.name} connected to Discord!')
await bot.change_presence(activity=discord.Game(name=f"Translating..."))

@bot.listen()
async def on_message(msg):
if msg.content == "": return
text_ = translate.translate(msg.content)[:-1]
if text_ == msg.content: return
else:
embed=discord.Embed(title="Translating...", description="Just a second...", color=0x545454)
msg_ = await msg.channel.send(embed=embed)
try:
emb=discord.Embed(title="Translated!", color=0x00ff08)
emb.add_field(name=f"Sent by {msg.author}", value=f"`Text: {msg.content}`\n`Translation: {text_}`", inline=False)
await msg_.edit(embed=emb)
except:
i = msg.content
if len(i) > 1800: i = "The message was too long to display!"
error=discord.Embed(color=0xff0000)
error.add_field(name="Error!", value=f"I couldn't translate **{msg.author}**'s message: `{i}`", inline=False)
await msg_.edit(embed=error)
return

bot.run(TOKEN)

AttributeError: 'NoneType' object has no attribute 'group' from Google translate API using pandas dataframe

I got the same error when I used googletrans v3 then I found an open github issue for this error. The suggested fix is to use
version googletrans-4.0.0rc1-py3.9.egg-info.

pip install 'googletrans==4.0.0rc1'

translate.py:

from googletrans import Translator
import pandas as pd

translator = Translator()

df = pd.DataFrame({'Spanish':['piso','cama']})
df['English'] = df['Spanish'].apply(translator.translate, src='es', dest='en').apply(getattr, args=('text',))
print(df)

I ran the code from the link you provided in the question and got the expected results.

Test done:
Sample Image

Sample Image



Related Topics



Leave a reply



Submit