0

I am creating a virtual assistant with Python, and when I do the part where it prints everything I speak, the code runs without errors:

import speech_recognition as sr

listener = sr.Recognizer()
try:
    with sr.Microphone() as source:
    print("listening...")
    voice = listener.listen(source)
    command = listener.recognize_google(voice)
    command = command.lower()
    if 'alexa' in command:
        print(command)
except:
    pass

But when I want to add the code which will make the virtual assistant speak, it shows errors:

import speech_recognition as sr
import pyttsx3

listener = sr.Recognizer()
engine = pyttsx3.init()
engine.say('I am alexa')
engine.say('What can I do for you?')
engine.runAndWait()
try:
    with sr.Microphone() as source:
        print("listening...")
        voice = listener.listen(source)
        command = listener.recognize_google(voice)
        command = command.lower()
        if 'alexa' in command:
            print(command)
except:
    pass

This is the error:

Traceback (most recent call last):
  File "C:\Users\User\PycharmProjects\romanticAlexa\venv\lib\site-packages\pyttsx3\__init__.py", line 20, in init
    eng = _activeEngines[driverName]
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\weakref.py", line 137, in __getitem__
    o = self.data[key]()
KeyError: None

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/User/PycharmProjects/romanticAlexa/main.py", line 5, in <module>
    engine = pyttsx3.init()
  File "C:\Users\User\PycharmProjects\romanticAlexa\venv\lib\site-packages\pyttsx3\__init__.py", line 22, in init
    eng = Engine(driverName, debug)
  File "C:\Users\User\PycharmProjects\romanticAlexa\venv\lib\site-packages\pyttsx3\engine.py", line 30, in __init__
    self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug)
  File "C:\Users\User\PycharmProjects\romanticAlexa\venv\lib\site-packages\pyttsx3\driver.py", line 52, in __init__
    self._driver = self._module.buildDriver(weakref.proxy(self))
  File "C:\Users\User\PycharmProjects\romanticAlexa\venv\lib\site-packages\pyttsx3\drivers\sapi5.py", line 30, in buildDriver
    return SAPI5Driver(proxy)
  File "C:\Users\User\PycharmProjects\romanticAlexa\venv\lib\site-packages\pyttsx3\drivers\sapi5.py", line 35, in __init__
    self._tts = comtypes.client.CreateObject('SAPI.SPVoice')
  File "C:\Users\User\PycharmProjects\romanticAlexa\venv\lib\site-packages\comtypes\client\__init__.py", line 250, in CreateObject
    return _manage(obj, clsid, interface=interface)
  File "C:\Users\User\PycharmProjects\romanticAlexa\venv\lib\site-packages\comtypes\client\__init__.py", line 188, in _manage
    obj = GetBestInterface(obj)
  File "C:\Users\User\PycharmProjects\romanticAlexa\venv\lib\site-packages\comtypes\client\__init__.py", line 112, in GetBestInterface
    interface = getattr(mod, itf_name)
AttributeError: module 'comtypes.gen.SpeechLib' has no attribute 'ISpeechVoice'
martineau
  • 119,623
  • 25
  • 170
  • 301
  • copy/pasted your code and I cannot repro. Using latest versions for both packages (eg no package specified in `requirements.txt`), and using `python 3.9.10`. Suggest you kill your virtualenv and reinstall both packages... – Edo Akse Mar 09 '22 at 17:50
  • [This](https://stackoverflow.com/a/67674044/9267296) might be useful though – Edo Akse Mar 09 '22 at 17:52
  • which packages should I reinstall? – Edvin Parmeza Mar 09 '22 at 17:57
  • so I hope you're using a `virtualenv`. If not, start doing so now. Read up [here](https://realpython.com/python-virtual-environments-a-primer/). That being said, the package giving the issue is likely the `pyttsx3` one. – Edo Akse Mar 09 '22 at 18:01
  • I am using Pycharm...I installed virtual env...should I create a new directory? I already have one directory, which is the project I am working on – Edvin Parmeza Mar 09 '22 at 19:17
  • are you actually using a virtualenv? If your answer to this is *I don't know*, please read up on what it does and how to use it. If you *are* using a virtualenv, my suggestion would be to deactivate it, completely remove it, create a new virtualenv, activate it, and reinstall the packages you're using. The good old *have you tried turning it off and on again* fixes so many issues... – Edo Akse Mar 09 '22 at 23:58

0 Answers0