【问题标题】:Python showing me error of " No module named gTTS ."Python 向我显示“没有名为 gTTS 的模块”的错误。
【发布时间】:2020-01-08 17:13:29
【问题描述】:

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

Traceback(最近一次通话最后一次):

文件“/Users/niallquadros/Desktop/voiceassistant.py”,第 1 行,<module> 从 gTTS 导入 gTTS

ModuleNotFoundError: 没有名为“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."

要做什么,我才能执行代码?

【问题讨论】:

  • 请阅读stackoverflow.com/help/minimal-reproducible-example。与您的问题相关的唯一代码行是“从 gTTS 导入 gTTS”;其余的是应该从问题中删除的噪音。要么你拼错了模块的导入名称('gtts' 更常见),安装了另一个 python 安装的模块,或者没有安装模块。您究竟是如何尝试安装该模块的,如果安装了,反应如何?

标签: macos-catalina python-3.7 gtts


【解决方案1】:

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

pip install gTTs

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

from gtts import gTTs

希望对您有所帮助!

【讨论】:

    【解决方案2】:

    确保使用正确的点安装它。因此,如果您使用 python3 运行它,那么它需要安装在 pip3 上..

    【讨论】:

      【解决方案3】:

      试试这个:

      from gtts import gTTS
      

      【讨论】:

      • 我试过了,但仍然显示与没有名为“gtts”的模块相同的结果,但谢谢。
      【解决方案4】:

      试试:

      from gtts import *
      

      它将导入所有内容。这对我有用。

      【讨论】:

        【解决方案5】:

        啊,那是因为您的文件名为 gtts.py,所以当您从 gtts import gTTS 执行操作时,Python 正在尝试导入您当前正在运行的文件。

        尝试将您的脚本重命名为其他名称,应该可以!

        【讨论】:

          【解决方案6】:

          代替:

          from gTTS import gTTS
          

          编写以下命令:

          from gtts import gTTS
          

          【讨论】:

          • 我试过了,但仍然显示与没有名为“gtts”的模块相同的结果
          【解决方案7】:

          如果以上答案不能解决您的问题,那么问题可能与 IDE(集成开发环境)有关。

          假设如果您使用像 Anaconda 这样的 IDE 并在 Jupyter notebook 上工作,那么您必须在 Anaconda 的环境中安装 gtts。

          • 只需转到 Anaconda 导航器并打开 CMD.exe 提示符
          • 类型:pip install gTTS or pip3 install gTTS pyttsx3 playsound

          现在,您可以导入 gtts。

          【讨论】:

            猜你喜欢
            • 2020-03-20
            • 2012-06-13
            • 2014-08-17
            • 2016-07-19
            • 2022-08-04
            • 2014-02-08
            • 2020-06-15
            • 2013-04-06
            • 2019-04-21
            相关资源
            最近更新 更多