3

I have pip installed googletrans and more or less copied this code off a video but for some reason it cannot find the module.

from googletrans import Translator
text=("How to convert some text to multiple languages")
destination_langauge={
    "Spanish": "es",
    "Chinese":"zh-CN",
    "Italian":"it"}
translator=Translator()
for key, value in destination_language.item():
    print(tranlator.translate(text, dest=value).text)

Any help will be greatly appreciated because I am struggling

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
Shoto
  • 33
  • 1
  • 1
  • 3

3 Answers3

3

Install googletrans with pip install googletrans. If you get a ModulNotFoundError you have not installed googletrans correctly.

from googletrans import Translator

text=("How to convert some text to multiple languages")
destination_language = {
    "Spanish": "es",
    "Chinese":"zh-CN",
    "Italian":"it"
}
translator=Translator()
for key, value in destination_language.items():
    print(translator.translate(text, dest=value).text)

You have multiple mistakes in your code. It's items() and not item() and the variable translator was misspelled in the last line.

The output of your program is:

Cómo convertir un texto a varios idiomas
如何将一些文本转换为多种语言
Come convertire del testo in più lingue
Habebit
  • 957
  • 6
  • 23
  • Thank you, I've made the changes but I am still getting the module not found error despite having installed it – Shoto Feb 21 '19 at 16:26
  • @Shoto What does the error say in detail? Please provide the full error message. – Habebit Feb 21 '19 at 16:31
  • Traceback (most recent call last): File "C:/Users/jordo/Desktop/CS Coursework/Tests/api hope.py", line 1, in from googletrans import Translator ModuleNotFoundError: No module named 'googletrans' – Shoto Feb 21 '19 at 16:32
  • Check if you have multiple python versions installed. And check your python program from different consoles/terminals/command prompts. – Habebit Feb 21 '19 at 16:39
  • Yes I did, manage to get it to work but doesn't work in Pycharm. The code works in python 3.6.2 but when I run it in pycharm the same error occurs – Shoto Feb 21 '19 at 17:52
  • Do you use a other version of python in pycharm than in you local console/terminal? – Habebit Feb 21 '19 at 17:54
  • Maybe, how would I change this? – Shoto Feb 21 '19 at 18:33
  • Have a look at this post: https://stackoverflow.com/questions/46354454/modulenotfounderror-error-with-pycharm-project-folder-recs He/She explained it very detailled. – Habebit Feb 21 '19 at 18:39
1

I think you need to pip install googletrans for you system python means you can deactivate the virtual environment and pip install googletrans and then activate the virtualenv again.

Syed Adnan Haider
  • 74
  • 1
  • 2
  • 11
0

I've got the same error and I found out the reason is that I have two Python versions installed (3.7 and 3.8) and the googletrans package was installed only for the one in system path (3.8). What I did is simply use the IDE with 3.8 (I use IDLE and PyCharm as IDE, both are easy to change Python interpreter).

xxg
  • 66
  • 5