'Python - NameError: name 'engine' is not defined / Driver not found

I have been trying to work on an AI Project in PyCharm using this video: https://www.youtube.com/watch?time_continue=179&v=rU_ppVsyJu8

Here is the code:

import sys
print(sys.path)

import speech_recognition as sr
import pyttsx3

try:
    engine = pyttsx3.init()
except ImportError:
    print("Driver not found")
except RuntimeError:
    print("Driver fails to init")

voices = engine.getProperty("voices")

for voice in voices:
    print(voice.id)

And there is an error:

enter image description here

Even though it says driver not found, I installed pyttsx3 right here:

enter image description here

I have been tackling this problem for a week, and with it I can't move on. If someone helped that would be appreciated.



Solution 1:[1]

You are not able to execute engine = pyttsx3.init(). That's why it's not recognizing the engine object. Try the code below. You will get the error message from Exception. Try to solve that error.

import sys
print(sys.path)

import speech_recognition as sr
import pyttsx3

try:
    engine = pyttsx3.init()
**except Exception as e:
    print(e)**
except ImportError:
    print("Driver not found")
except RuntimeError:
    print("Driver fails to init")

voices = engine.getProperty("voices")

for voice in voices:
    print(voice.id)

Solution 2:[2]

It is very easy to fix. I also use Pycharm and other code editors. Just open YOUR Project and if your code is still the same just do this:

Put this line: engine = pyttsx3.init() after import pyttsx3 or just remove your full code and copy/paste this code:

import sys
print(sys.path)

import speech_recognition as sr
import pyttsx3
engine = pyttsx3.init()

try:
    engine = pyttsx3.init()
except Exception as e:
    print(e)
except ImportError:
    print("Driver not found")
except RuntimeError:
    print("Driver fails to init")

voices = engine.getProperty("voices")

for voice in voices:
    print(voice.id)

I always get's these kind of errors. OK, it's the fix for this problem now. I am going to fix the error:

NameError: name 'audio' is not defined

Hope this works.

Solution 3:[3]

Very simple and easy solution for this pyaudio error:

Step 1: Visit this link https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyaudio

Step 2: Search your python version example, I have python 3.9.6 win32(amd64) then I selected and downloaded cp39 win32(amd64) and installed it using simple click.

Step 3: Go on that downloaded folder where your file has downloaded and then open there Powershell and then type

pip install .\PyAudio-0.2.11-cp39-cp39-win_amd64.whl

Now by following these steps, I think your error will be resolved.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Niels Henkens
Solution 2 Mikhail Zakharov
Solution 3 Sofia