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

Python Jarvis项目报错求助:sapi5驱动及comtypes模块问题

问题解决:Jarvis项目运行报错(KeyError: 'sapi5' & ModuleNotFoundError: 'comtypes')

核心问题分析

从报错路径/Library/Frameworks/Python.framework能看出你使用的是Mac系统,但代码里指定的sapi5是Windows专属的语音驱动,Mac不支持;另外comtypes是Windows平台的依赖库,Mac环境完全不需要。

分步解决方案

1. 替换pyttsx3的驱动(适配Mac)

把初始化pyttsx3的代码从:

engine = pyttsx3.init('sapi5')

改成Mac兼容的驱动:

engine = pyttsx3.init('nsss')  # nsss是MacOS原生语音驱动

2. 清理不必要的依赖

不需要安装comtypes,如果之前误装了可以卸载:

pip uninstall comtypes

3. 修复代码拼写错误(避免后续运行异常)

  • speak('Goog Morning!') → speak('Good Morning!')
  • speak('Goog Afternoon!') → speak('Good Afternoon!')
  • with sr.Microphone() as sourse: → with sr.Microphone() as source:(变量名拼写错误)
  • query = r.recognize_google(audio,language='en-ln') → query = r.recognize_google(audio,language='en-us')(语言代码错误,en-ln不存在)
  • def sebdEmail(to,content): → def sendEmail(to,content):(函数名拼写错误,后续调用需同步修改)

4. 验证Mac系统权限

确保Python程序有麦克风访问权限:

  • 打开「系统设置」→「隐私与安全性」→「麦克风」,勾选你的Python解释器

修改后的完整代码示例

import pyttsx3
import speech_recognition as sr
import datetime
import webbrowser
import wikipedia
import os
import smtplib

# 适配Mac的nsss驱动
engine = pyttsx3.init('nsss')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)

def speak(audio):
    engine.say(audio)
    engine.runAndWait()

def wishMe():
    hour = int(datetime.datetime.now().hour)
    if hour >= 0 and hour < 12:
        speak('Good Morning!')
    elif hour >= 12 and hour < 18:
        speak('Good Afternoon!')
    else:
        speak('Good Evening!')
    speak('Hi, how can I help you?')

def takeCommand():
    r = sr.Recognizer()
    # 修复变量名拼写错误
    with sr.Microphone() as source:
        print('Listening..')
        r.pause_threshold = 1
        audio = r.listen(source)
    try:
        print('Recognizing..')
        # 修复语言代码错误
        query = r.recognize_google(audio, language='en-us')
        print(f'User Said {query}\n')
    except Exception as e:
        print('Say That Again')
        return 'None'
    return query

# 修复函数名拼写错误
def sendEmail(to, content):
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.ehlo()
    server.starttls()
    server.login('email@gmail.com', '********')
    server.sendmail('email@gmail.com', to, content)
    server.close()

if __name__ == '__main__':
    wishMe()
    while True:
        query = takeCommand().lower()

        if 'wikipedia' in query:
            speak('Searching it..')
            query = query.replace('wikipedia', '')
            results = wikipedia.summary(query, sentences=2)
            speak('According to wikipedia..')
            print(results)
            speak(results)
        elif 'open youtube' in query:
            webbrowser.open('youtube.com')
        elif 'open google' in query:
            webbrowser.open('google.com')
        elif 'the time' in query:
            strTime = datetime.datetime.now().strftime('%H:%M:%S')
            speak(f'Sir, the time is {strTime}')
        elif 'email to friend' in query:
            try:
                speak('What would you like me to say?')
                content = takeCommand()
                to = 'emailw@gmail.com'
                # 同步修改函数调用
                sendEmail(to, content)
                speak('Sir, it\'s done')
            except Exception as e:
                print(e)
                speak('Sorry, I\'m not able to send this one')

内容的提问来源于stack exchange,提问作者Count of Monte Cristo

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 20:41:07