首页 > 解决方案 > 为什么 Python 模块不能在命令行中工作?

问题描述

我的笔记本电脑上安装了 Python 3.8.2。我安装了各种模块并在下面的代码中导入它们。

 import wikipediaapi
    from gtts import gTTS
    import speech_recognition as sr
    import os
    import datetime
    import calendar
    import warnings
    import random
    # ignore any warnings
warnings.filterwarnings('ignore')


# audio -> command -> response (text) -> response (speech)

# records audio and return it as string

def recordAudio():
    r = sr.Recognizer  # creating a recognizer object

    # open the mic
    with sr.Microphone() as src:
        print('say something!')
        audio = r.listen(self=None, source=src, timeout=None, phrase_time_limit=None, snowboy_configuration=None)

    # use google speech recog

    data = ''
    try:
        r.recognize_google(self=None, audio_data=audio, key=None, language='en', show_all=False)
        print('You said: ' + data)
    except sr.UnknownValueError as s:
        print('Your audio could not be recognized' + s)
    except sr.RequestError as e:
        print('Request results from Google Speech Recog error' + e)

    return data


def assistantResponse(string):
    print(string)
    myObj = gTTS(text=string, lang='en, fr', slow=False)

    # save the converted audio to file.

    myObj.save('assistant_response.mp3')

    # Play the file
    os.system('start assistant_response.mp3')


# A function for wake word(s) or phrase
def wakeWord(string):
    WAKE_WORDS = ['hey Apoorva', 'okay Apoorva']  # A list of wake words

    text = string.lower()

    for phrase in WAKE_WORDS:
        if phrase in text:
            return True

    # IF wake word isn't found in the text, comp returns false.
    return False

    # A function to reutrn the current date


def getDate():
    now = datetime.datetime.now()
    my_date = datetime.datetime.today()
    weekday = calendar.day_name[my_date.weekday()]
    monthNum = now.month
    dayNum = now.day

    # a list of months

    month_names = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August',
                   'September', 'October', 'November', 'December']

    return 'Today is ' + month_names[monthNum - 1], ', ' + dayNum, 'st, ' + weekday


def greetingUser(string):
    GREETINGS_INPUT = ['hello', 'hola', 'hi', 'greetings', 'wassup']
    GREETINGS_OUTPUT = ['hello, matey!', 'oh hi!', 'hey there!']

    if GREETINGS_INPUT in string:
        rando = random.randint(0, 2)
        return GREETINGS_OUTPUT[rando]


def wikiSearch(string):
    wordList = string.split()

    for i in range(0, len(wordList)):
        if i + 3 <= len(wordList) - 1 and wordList[i].lower() == 'who' or 'what' and wordList[i + 1].lower() == 'is':
            return wordList[i + 2] + ' ' + wordList[i + 3]


while True:

    # Record Audio
    text = recordAudio()
    response = ''

    # Check fro wake word/phrase

    if (wakeWord(text)) == True:

        response = response + greetingUser(text)

        if ('date' in text):
            get_date = getDate()
            response = response + ' ' + get_date

        if ('who is' or 'what is' in text):
            person = wikiSearch(text)
            wiki = wikipedia.summary(person, sentences=2)
            response = response + ' ' + wiki

            assistantResponse(response)

当我尝试在 cmd 中执行 python 函数时,它显示以下错误: line 1, in import wikipediaapi ImportError: No module named wikipediaapi

当我用维基百科替换它时,它显示相同,也适用于 gtts 模块。请帮忙。提前致谢

标签: python

解决方案


  1. 用于pip3 list获取已安装模块的列表

  2. 如果安装的模块不在列表中,请尝试使用以下命令重新安装模块,

    pip3 install wikipedia-api

通过此链接https://pypi.org/project/Wikipedia-API/


推荐阅读