简体   繁体   中英

Error using googletrans api in python translate() missing 1 required positional argument: 'self'

I want to translate some text using googletrans. This is my code:

inputtext = "Ich mag Schokolade"

srclang = "de"
dstlang = "en"

translation = Translator.translate(text=inputtext, src=srclang, dest=dstlang)

But when I run it, this Error comes up:

translate() missing 1 required positional argument: 'self'

You need to initialize an instance of Translator :

translator = Translator()
inputtext = "Ich mag Schokolade"

srclang = "de"
dstlang = "en"

translation = translator.translate(text=inputtext, src=srclang, dest=dstlang)

translate(...) is not a static method of Translator so you need to call it on an instance rather than class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM