I'm trying to use Google text-to-speech. When I run the code, I get the error: from gtts import gTTS ModuleNotFoundError: No module named 'gtts'.
I am used a Raspberry Pi 3. I used pip install gtts and pip3 install gtts and these worked successfully. I've uninstalled gtts and re-installed it several times. The current version is gtts 2.5.1 that I have installed. I am using python 3.9.2. When I start python3 from the terminal and type "from gtts import gTTS", it seems to work because there is no error message. But when I try to run any script with that line, it comes up with the error. This is the latest script:
import os
from gtts import gTTS
text_to_read = "This is just a test."
language = 'en'
slow_audio_speed=False
filename = "speech.mp3"
def reading_from_string():
audio_created = gtts(text_to_read, lang=language,slow=slow_audio_speed)
audio_created.save(filename)
os.system(f'start {filename})
I have also run pip3 list to see if it is listed as one of the packages, and it's there, gTTS 2.5.1.
I even tried pip list and it is there, version 2.5.1. My pip version is 20.3.4 and pip3 is the same.
I don't know if this helps, but in the terminal I am able to put in the command gtts-cli 'hello' --ouput hello.mp3. Then using aplay hello.mp3 it plays a sound, but it's just noise/static. So it seems like gtts is there, but python can't find it?
I've been searching the internet for a solution to this "no module name 'gtts'" and it seems to be a common problem with different solutions! Hopefully you can help me.
I tried located gtts using pip3 list and it shows that it is there, in the list of installed packages. I have tried uninstalling and re-installing gtts. I have tried searching for answers on the internet, but none seem to be related to my problem, or they don't have an answer. I'm thinking maybe pip3 has installed gtts in the wrong place, or maybe my python version just isn't compatible with the current version of gtts? This may be the case because I have read some posts where they used an older version of python and got it to work.
I don't want to try different environments like Anaconda, I just want to do it on the Raspberry Pi because I'm following the instructions from a book entitled: "Building a Virtual Assistant for Raspberry Pi".
I've also tried using pyenv to try different versions of python, but the last 3 attempts at installing a different python version (2.7.11, 2.7.12 and 3.1.0) have all failed. I thought if I could try running gtts in python 2.7.11 like in the book, and it worked, it would prove the problem was with the version of python and not the module.