0

所以我有一个更大的问题,但我解决了这个问题。现在,我有这个错误命令:

pygame 2.0.1 (SDL 2.0.14, Python 3.7.9) 来自 pygame 社区的问候。https://www.pygame.org/contribute.html Traceback(最近一次调用最后一次):文件“c:/Users/danku/jarvis.py”,第 16 行,在 engine = pyttsx3.init('sapi5') AttributeError : 模块 'pyttsx3' 没有属性 'init'

我尝试了一切(安装了 pygame、pypiwin32、pywintypes),但我无法弄清楚。这是我心爱的代码(不要笑它的 jarvis 代码):

#alap
import pyttsx3
import datetime
import speech_recognition as sr
import wikipedia
import webbrowser
import os
import pywhatkit
import pyjokes
import subprocess
import pywintypes
import win32com.client
import pygame 


engine = pyttsx3.init('sapi5')

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

def time():
    Time = datetime.datetime.now().strftime("%H:%M:%S")
    speak(Time)

def date():
    year = int(datetime.datetime.now().year)
    month = int(datetime.datetime.now().month)
    date = int(datetime.datetime.now().day)
    speak(date)
    speak(month)
    speak(year)

def wishme():
    speak("Welcome back sir! All system are ready for work!")
    speak("the current time is")
    time()
    speak("The current date is")
    date()
    hour = datetime.datetime.now().hour
    if hour >= 6 and hour<12:
        speak("Good morning sir!")
    elif hour >=12 and hour<18:
        speak("Good afternoon sir!")
    elif hour >=18 and hour<24:
        speak("Good evening sir!")
    else:
        speak("Good night sir!")

    speak("Jarvis at your service. Please tell me 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(query)

    except Exception as e:
        print(e)
        speak("Say that again")

        return "none"
    return query

if __name__ == "__main__":
    wishme()
    while True:
        query = takeCommand().lower()

        if 'wikipedia' in query:  #if wikipedia found in the query then this block will be executed
            speak('Searching Wikipedia...')
            query = query.replace("wikipedia", "")
            results = wikipedia.summary(query, sentences=2) 
            speak("According to Wikipedia")
            print(results)
            speak(results)
``
Also i'm using python 2.71, and latest of pip.
4

1 回答 1

0

这通常是因为您将 Python 文件命名为与您要导入的模块相同的名称并导致循环引用。尝试更改文件的名称。它应该可以解决问题。

于 2022-02-17T20:05:54.470 回答