i am making voice assistant in python. I found example in youtube. Then i try it but when i run this code i am getting errors. Pyttsx3 can't runnning. I can't find solution. I need help. I am using MacBook M2.
Here's my code:
import pyttsx3
import pywhatkit
import datetime
import speech_recognition as sr
listener = sr.Recognizer()
engine = pyttsx3.init()
def talk(text):
engine.say(text)
engine.runAndWait()
def input_instruction():
global instruction
try:
with sr.Microphone() as origin:
print("I'm listening...")
speech = listener.listen(origin)
instruction = listener.recognize_sphinx(speech)
instruction = instruction.lower()
if "Jarvis" in instruction:
instruction = instruction.replace('jarvis', '')
print(instruction)
except:
pass
def play_Jarvis():
instruction = input_instruction()
print(instruction)
if "play" in instruction:
song = instruction.replace('play', "")
talk("Playing " + song)
pywhatkit.playonyt(song)
elif 'time' in instruction:
time = datetime.datetime.now().strftime('%I:%M%p')
talk('Time is' + time)
elif 'tarih' in instruction:
date = datetime.datetime.now().strftime('%d /%m /%Y')
talk('Today's date' + date)
elif 'how are you' in instruction:
talk('Thanks boss and you?')
elif 'what is your name' in instruction:
talk('my name is Jarvis how can i help you')
else:
talk('Please repeat')
play_Jarvis()
Then i'm getting these errors:
Traceback (most recent call last):
File "/Users/akyel/PycharmProjects/sesliAsistan/.venv/lib/python3.12/site-packages/pyttsx3/__init__.py", line 20, in init
eng = _activeEngines[driverName]
~~~~~~~~~~~~~~^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/weakref.py", line 136, in __getitem__
o = self.data[key]()
~~~~~~~~~^^^^^
KeyError: None
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/akyel/PycharmProjects/sesliAsistan/main.py", line 7, in <module>
engine = pyttsx3.init()
^^^^^^^^^^^^^^
File "/Users/akyel/PycharmProjects/sesliAsistan/.venv/lib/python3.12/site-packages/pyttsx3/__init__.py", line 22, in init
eng = Engine(driverName, debug)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/akyel/PycharmProjects/sesliAsistan/.venv/lib/python3.12/site-packages/pyttsx3/engine.py", line 30, in __init__
self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/akyel/PycharmProjects/sesliAsistan/.venv/lib/python3.12/site-packages/pyttsx3/driver.py", line 50, in __init__
self._module = importlib.import_module(name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/importlib/__init__.py", line 90, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 995, in exec_module
File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
File "/Users/akyel/PycharmProjects/sesliAsistan/.venv/lib/python3.12/site-packages/pyttsx3/drivers/nsss.py", line 12, in <module>
class NSSpeechDriver(NSObject):
File "/Users/akyel/PycharmProjects/sesliAsistan/.venv/lib/python3.12/site-packages/pyttsx3/drivers/nsss.py", line 13, in NSSpeechDriver
@objc.python_method
^^^^
NameError: name 'objc' is not defined. Did you mean: 'object'?
Please help me...
I reinstalled everything but i can't solved.