pythonpython-3.xraspberry-pi3raspbianespeak

I have a problem with espeak on Raspbian: No module named 'espeak'



Hi! So my problem is: I'm trying to make my Pi 3 speaking, I looked up multiple articles, but none of them could help unfortunately.


My code line: from espeak import espeak
And I get an error:
File "/home/pi/ttstest.py", line 9, in
from espeak import espeak
ModuleNotFoundError: No module named 'espeak'

I'm using Raspbian on Raspberry Pi 3.
I have espeak and python-espeak istalled as well.
I used this code in terminal to test the espeak: espeak "Hello World" 2>/dev/null It works, I'm hearing the speech.

If someone please could help me, I would be more than grateful! :)


Solution

  • Method using espeak-python

    Install espeak-python

    sudo apt install espeak-python

    Then,you can play text as following

    from espeak import espeak
    
    espeak.set_voice("en")
    
    espeak.synth("hello")
    
    while espeak.is_playing:
        pass
    

    for more information using this method click here

    Method using espeak

    Alernatively,You can run play using espeak command in python

    install espeak

    sudo apt install espeak

    Then,you can play text as following

    import os
    text="this is demo text"
    os.system('espeak "'+text+'"')
    

    Here text is wrapped with double quotes because without it the espeak command will only take first word in this case i.e 'this'

    You can also define other arguments as in terminal like speed,voice,word gap etc.

    Example using speed of 200 WPM(words per minute)

    import os
    text="this is demo text"
    os.system('espeak -s 200 "'+text+'"')
    

    You may also directly pass string inside double quotes

    import os
    os.system('espeak -s 200 "this is demo text"')
    

    for more information using this method click here