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

Mac M2芯片Python 3.11开发TTS遇报错,求助解决方案

类JARVIS程序语音模块报错修复方案

一、pyttsx3相关问题修复

  • 补全系统依赖:
    Windows系统执行 pip install pywin32,pyttsx3依赖该库调用Windows语音引擎;Linux需安装espeak(包管理器命令如sudo apt install espeak);macOS确保系统自带语音引擎启用。
  • 重装兼容版本:
    完全卸载现有库后重新安装稳定版本:
    pip uninstall -y pyttsx3 speechrecognition
    pip install pyttsx3==2.90 speechrecognition==3.10.0
    

二、PyAudio安装替代方案

若Python3.11安装PyAudio失败,可采用以下两种方式:

  • Windows用户直接安装适配版本:pip install pyaudio-win32
  • 跨平台替代:安装sounddevice库配合语音识别,命令为 pip install sounddevice,并修改代码中麦克风初始化逻辑,指定使用sounddevice作为音频输入源。

三、代码层面优化(避免初始化报错)

  • 指定pyttsx3引擎驱动:
    import pyttsx3
    # 根据系统选择驱动:Windows=sapi5,Linux=espeak,macOS=nsss
    engine = pyttsx3.init(driverName='sapi5')
    
  • 添加语音识别异常捕获,定位具体错误:
    import speech_recognition as sr
    recognizer = sr.Recognizer()
    with sr.Microphone() as source:
        recognizer.adjust_for_ambient_noise(source)
        try:
            audio = recognizer.listen(source, timeout=5)
            text = recognizer.recognize_google(audio)
            print(f"识别结果: {text}")
        except sr.UnknownValueError:
            print("无法解析语音内容")
        except sr.RequestError as e:
            print(f"语音识别服务请求失败: {str(e)}")
        except Exception as e:
            print(f"麦克风或引擎初始化错误: {str(e)}")
    

内容的提问来源于stack exchange,提问作者Sean Bray

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 06:42:16