You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用pyttsx3调用sapi5报错:无法从_ctypes导入COMError

问题:修复pyttsx3语音助手的ImportError: cannot import name 'COMError' from '_ctypes'错误

正在开发桌面语音助手,使用pyttsx3实现语音播报功能时触发上述错误,已尝试pip3 install pyttsx3、pip3 uninstall pyttsx3、pip3 install pyttsx3==2.6、pip3 install pypiwin32等操作,问题仍未解决。

报错栈信息

File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyttsx3/__init__.py", line 20, in init
    eng = _activeEngines[driverName]
          ~~~~~~~~~~~~~~^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/weakref.py", line 136, in __getitem__
    o = self.data[key]()
        ~~~~~~~~~^^^^^
KeyError: 'sapi5'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/simrankataria/Desktop/test.py", line 2, in <module>
    Assistant= pyttsx3.init('sapi5')
               ^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyttsx3/__init__.py", line 22, in init
    eng = Engine(driverName, debug)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyttsx3/engine.py", line 30, in __init__
    self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyttsx3/driver.py", line 50, in __init__
    self._module = importlib.import_module(name)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1149, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyttsx3/drivers/sapi5.py", line 1, in <module>
    import comtypes.client  # Importing comtypes.client will make the gen subpackage
    ^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/comtypes/__init__.py", line 28, in <module>
    from _ctypes import COMError
ImportError: cannot import name 'COMError' from '_ctypes' (/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/lib-dynload/_ctypes.cpython-311-darwin.so)

测试代码

import pyttsx3
Assistant= pyttsx3.init('sapi5')
voices = Assistant.getProperty('voices')
print(voices)
Assistant.setProperty('voices',voices[1].id)

def Speak(audio):
    print("  ")
    Assistant.say(audio)
    print("  ")
    Assistant.runAndWait()

Speak("Blah")

修复方案

  • 更换适配系统的语音驱动:sapi5是Windows专属的语音驱动,从路径判断你使用的是macOS,应改用macOS原生的nsss驱动,修改初始化代码:

    Assistant= pyttsx3.init('nsss')
    
  • 调整依赖版本适配Python 3.11:Python 3.11与pyttsx3旧版本兼容性不佳,执行以下命令重新安装适配版本:

    1. 卸载现有冲突依赖:
      pip3 uninstall -y pyttsx3 comtypes pypiwin32
      
    2. 安装兼容的pyttsx3版本:
      pip3 install pyttsx3==2.90
      

    (注:pypiwin32是Windows依赖,macOS无需安装)

  • 验证系统语音配置:打开macOS系统偏好设置,进入「辅助功能-语音」,确保已启用至少一个系统语音,避免获取语音列表时出错。

内容的提问来源于stack exchange,提问作者Simrann Kataria

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 21:05:12