0

While using the pyttsx3 module in my python 3.8.5 virtual environment by this code:

import pyttsx3

def speak(speak):
    engine = pyttsx3.init()
    voices = engine.getProperty('voices')
    engine.setProperty('voice', voices[1].id)
    engine.getProperty('rate')
    engine.setProperty('rate', 155)
    engine.say(speak)
    engine.runAndWait()

speak("Hello")

I'm getting this error on compilation of the above code:

Traceback (most recent call last):
  File "D:\virtualenv\venv\lib\site-packages\pyttsx3\__init__.py", line 20, in init
    eng = _activeEngines[driverName]
  File "c:\users\CurrentUser\appdata\local\programs\python\python38-32\lib\weakref.py", line 131, in __getitem__
    o = self.data[key]()
KeyError: None

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "d:/virtualenv/hehe.py", line 12, in <module>
    speak("Hello")
  File "d:/virtualenv/hehe.py", line 4, in speak
    engine = pyttsx3.init()
  File "D:\virtualenv\venv\lib\site-packages\pyttsx3\__init__.py", line 22, in init
    eng = Engine(driverName, debug)
  File "D:\virtualenv\venv\lib\site-packages\pyttsx3\engine.py", line 30, in __init__
    self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug)
  File "D:\virtualenv\venv\lib\site-packages\pyttsx3\driver.py", line 50, in __init__
    self._module = importlib.import_module(name)
  File "c:\users\CurrentUser\appdata\local\programs\python\python38-32\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "D:\venv\lib\site-packages\pyttsx3\drivers\sapi5.py", line 1, in <module>
    import comtypes.client  # Importing comtypes.client will make the gen subpackage
  File "D:\virtualenv\venv\lib\site-packages\comtypes\__init__.py", line 1176, in <module>
    class IPersist(IUnknown):
  File "D:\virtualenv\venv\lib\site-packages\comtypes\__init__.py", line 1180, in IPersist
    COMMETHOD([], HRESULT, 'GetClassID',
  File "D:\virtualenv\venv\lib\site-packages\comtypes\__init__.py", line 1099, in COMMETHOD
    from comtypes.automation import VARIANT
  File "D:\virtualenv\venv\lib\site-packages\comtypes\automation.py", line 4, in <module>
    import decimal
  File "c:\users\CurrentUser\appdata\local\programs\python\python38-32\lib\decimal.py", line 3, in <module>
    from _decimal import *
AttributeError: module 'numbers' has no attribute 'Number'

Everytime I run the given python code, the above error occurs and I'm using VS code as my coding IDE in Windows 10

ApidBoy
  • 31
  • 1
  • 5

2 Answers2

0

Take the pyttsx3.init() out of the function. It is not to called repeateadly. It is to be called only once.

import pyttsx3
engine = pyttsx3.init()
def speak(word):
   #set engine properties
   engine.say(word)
   engine.runAndWait()

speak('Speak')
Ekure Edem
  • 310
  • 2
  • 10
0

This should work:

import pyttsx3
engine = pyttsx3.init()

def speak(word):
    voices = engine.getProperty('voices')
    engine.setProperty('voice', voices[1].id)
    rate = engine.getProperty('rate')
    engine.setProperty('rate', 155)
    engine.say(word)
    engine.runAndWait()

speak("Hello")
Ameya Uppina
  • 131
  • 1
  • 3
  • 13