简体   繁体   English

如何修复 python 3.8 中的 pyttsx3 模块错误

[英]How to fix pyttsx3 module error in python 3.8

While using the pyttsx3 module in my python 3.8.5 virtual environment by this code:通过此代码在我的python 3.8.5虚拟环境中使用pyttsx3模块时:

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每次运行给定的 python 代码时,都会出现上述错误,我使用 VS 代码作为 Windows 10 中的编码 IDE

Take the pyttsx3.init() out of the function. It is not to called repeateadly.从 function 中取出pyttsx3.init() 。不要重复调用它。 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')

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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM