0

我正在尝试使用此代码在 python 中制作语音助手

import os
from gtts import gTTs
import time
import playsound
import speech_recognition as sr



def speak(text):
    tts = gTTS(text=text, lang="en")
    filename = "voice.mp3"
    tts.save(filename)
    playsound.playsound(filename)

def get_audio():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        audio = r.listen(source)
        said = ""

        try:
            said = r.recognize_google(audio)
            print(said)
        except Exception as e:
            print("Exception: " + str(e))

    return said


text = get_audio()

if "who are you" in text:
    speak(" I am Friday the virtual assistant")

当我运行它时,它显示此错误 ImportError: cannot import name gTTS

任何帮助都会很棒:)

编辑:我已将其更改为 gTTS,但仍然收到 ImportError: cannot import name gTTS

4

4 回答 4

3

尝试更换

from gtts import gTTs

from gtts import gTTS

(注意大写S

于 2020-07-22T12:56:52.117 回答
1

我也遇到过这个问题,因为我将我的 python 文件命名为 gtts.py,所以我将文件名更改为其他名称,它停止给出错误。

于 2021-04-03T00:29:27.680 回答
0

首先,您必须使用此命令安装 gtts 模块,方法是转到终端并键入以下行:

pip install gTTS

安装后,像这样导入:

from gtts import gTTS
于 2020-12-26T12:51:34.373 回答
0

我得到了同样的错误。只要确保您使用的是正确的 python 解释器。

于 2021-09-26T07:53:09.313 回答