-2

So I was following a simple tutorial on youtube, and no matter what I do I keep getting the same issue.

Here is the code I used.

import speech_recognition as sr
import pyttsx3


voices = []
engine = pyttsx3.init()

voices = engine.getProperty('voices')
for voice in voices:
    print(voice.id)

I am writing this in sublimeText3. Everytime I build this, I get the same error.

File "C:\Users\This PC\Desktop\Py\introTest.py", line 14, in voices = engine.getProperty('voices') NameError: name 'engine' is not defined

Not sure why it is saying "engine" is not defined. I clearly have defined it under the try. Any help would be appreciated.

After removing the try/excepts I have a lot of new errors. Here is the build log.

Traceback (most recent call last): File "C:\Users\This PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pyttsx3__init__.py", line 44, in init eng = _activeEngines[driverName] File "C:\Users\This PC\AppData\Local\Programs\Python\Python37-32\lib\weakref.py", line 137, in getitem o = self.datakey KeyError: None

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:\Users\This PC\Desktop\Py\demo.py", line 7, in engine = pyttsx3.init() File "C:\Users\This PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pyttsx3__init__.py", line 46, in init eng = Engine(driverName, debug) File "C:\Users\This PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pyttsx3\engine.py", line 52, in init self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug) File "C:\Users\This PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pyttsx3\driver.py", line 75, in init self._module = importlib.import_module(name) File "C:\Users\This PC\AppData\Local\Programs\Python\Python37-32\lib\importlib__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 1006, in _gcd_import File "", line 983, in _find_and_load File "", line 967, in _find_and_load_unlocked File "", line 677, in _load_unlocked
File "", line 728, in exec_module File "", line 219, in _call_with_frames_removed File "C:\Users\This PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pyttsx3\drivers\sapi5.py", line 3, in import win32com.client ModuleNotFoundError: No module named 'win32com' [Finished in 0.1s]

KevinM1990112qwq
  • 715
  • 2
  • 10
  • 23

5 Answers5

1

After seeing your full error log dump (without the try block hiding errors), in the last bit is the text:

line 3, in import win32com.client ModuleNotFoundError: No module named 'win32com' [Finished in 0.1s]

I would suggest looking at Jefferson Puchalski's answer again. (hint: it's telling you you're missing a module that pyttsx3 is dependant on)


You are attempting to assign engine = pyttsx3.init(), but when/if it fails, you then declare voices = engine.getProperty('voices'). But as the try block has failed, engine has not been declared.

Below, I assign engine = None, and skip the else form the try block; instead using a conditional to determine if it's None or not (it worked correctly on creating engine).

import speech_recognition as sr
import pyttsx3


voices = []
engine = None

try:
    engine = pyttsx3.init()
except ImportError:
    print('Import Issue')
except RuntimeError:
    print('Runtime Issue')

if (engine is not None):
    voices = engine.getProperty('voices')
    for voice in voices:
        print(voice.id)
else:
    print("Something went wrong")
Matthew Moore
  • 866
  • 7
  • 10
0

Try input the import in the try/catch block , and check if you got undefined after import, so throw exception. Looking in module official website, look your console logs for some of this erros,

Fixes for possible errors : No module named win32com.client No module named win32 No module named win32api

and try this

pip install pypiwin32

Addtiional show your logs to we understand better the situation!

0
import speech_recognition as sr
import pyttsx3

voices = [] 
try:
    engine = pyttsx3.init()
except ImportError:
    print('Requested driver is not found')
    engine = None
except RuntimeError:
    print('Driver fails to initialize')
    engine = None

if engine is None:
    print('Something went wrong')

else:
    voices = engine.getProperty('voices')
    for voice in voices:
        print(voice.id)
Niels Henkens
  • 2,553
  • 1
  • 12
  • 27
Jake
  • 1
0

on Ubuntu I had the same issue. Easiest fix, ever:

sudo apt install libespeak1

Sage
  • 193
  • 1
  • 7
0

Hi I've faced a similar issue and this is how i managed to change voice with ease

import pyttsx3

voices = []

eng = pyttsx3.init()

voices = eng.getProperty('voice')

eng.setProperty('voice',voices[1].id)
eng.say("Some text here")
eng.runAndWait()