python - 为什么 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 模块。请帮忙。提前致谢
解决方案
用于
pip3 list
获取已安装模块的列表如果安装的模块不在列表中,请尝试使用以下命令重新安装模块,
pip3 install wikipedia-api
推荐阅读
- elasticsearch - How can I execute ElasticSearch query on multiple indices with nested mapping
- php - 未找到 Laravel 公共存储文件夹
- javascript - 离开页面之前的 Javascript,除非单击按类或 ID 的元素
- c# - How to collect values into an array and pass the collection to datagridview in C#?
- database - 使用扇出写入方法根据隐私设置(PostgreSQL、Redis)更新 Feed
- dynamics-crm - 有账户区域的切片器,但需要通过切片器的相关实体字段过滤数据
- git - 在我的网络重新安排后,无法将 Visual Studio 解决方案重新连接到 GIT
- javascript - 了解 Chrome DevTools 内存性能图中的模式
- sql - 将 UNION ALL 与 GROUP BY 结合
- sql-server - SSRS 2017 排列多个 Tablix 和矩阵