pythonpyaudiopyttsx

Python simple tutorial variable not defined issue


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]


Solution

  • 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!