gpt4 book ai didi

macos-catalina - Python 显示错误 "No module named gTTS ."

转载 作者:行者123 更新时间:2023-12-01 21:50:04 24 4
gpt4 key购买 nike

我想为将使用 gTTS 的语音助手创建 python 代码。我运行了代码,但它没有像它显示的那样工作,

Traceback (most recent call last):

File "/Users/niallquadros/Desktop/voiceassistant.py", line 1, in <module> from gTTS import gTTS

ModuleNotFoundError: No module named 'gTTS'

相反,它应该给我结果,并充当语音助手。

这是我在 MacBook Air 2019 上用 python (3.7.4) 编写的代码

from gTTS import gTTS
import speech_recognition as sr
import os
import webbrowser
import satplib

def talkToMe(audio):
print (audio)
tts = gTTs(text=audio, lang='en')
tts.save('audio.mp3')
os.system('mpg123 audio.mp3')

#Listen for commands
def myCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print('I am ready for your next command')
r.pause_threshold = 1
r.adjust_for_ambient_noise(source, duration = 1)
audio = r.listen(source)

try:
command = r.recognize_google(audio)
print('You said: ' + command + '/n')

#loop back to continue to listen for commands

except sr.UnknownValueError:
assistant(myCommand())

return command

#if statesments for executing commands
def assistant(command):

if 'open Reddit python' in command:
chrome_path = '/user/bin/google.chrome'
url = 'https://www.reddit.com/r/python'
webbrowser.get(chrome_path).open(url)

if 'what\'s up' in command:
talkToMe('Chillin Bro')

while True:
assistant(myCommand())

在我的终端中,它显示我已经安装了 gtts。

(base) Nialls-MacBook-Air:~ niallquadros$ pip search gTTS
gTTS (2.1.0) - gTTS (Google Text-to-Speech), a Python library and CLI
tool to interface with Google Translate text-to-speech
API
INSTALLED: 2.1.0 (latest)
Django-Gtts (0.2) - gTTS google text-to-speech django app
Flask-gTTS (0.12) - gTTS Google text to speech flask extension
gTTS-token (1.1.3) - Calculates a token to run the Google Translate text to
speech
INSTALLED: 1.1.3 (latest)
wired-tts (0.2.0) - "gTTS based Wired story reader."

该怎么做才能执行代码?

最佳答案

我希望您首先安装了库(Google Text-to-Speech)如果没有,请通过以下命令在 python 中安装它:

pip install gTTs

安装库后,它可以在您的代码中使用,请注意字符的大小写。

from gtts import gTTs

希望对您有所帮助!

关于macos-catalina - Python 显示错误 "No module named gTTS .",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59650762/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com