python - 导入自定义函数时出错
问题描述
当我尝试从文件中导入函数时,它无法识别我之前导入的模块,而直接定义时则没有问题。
该模块作为 sr 导入。我应该导入它的函数体还是有任何其他技巧。
# doesn't work
import speech_recognition as sr
r = sr.Recognizer()
from Chatfunctions import Listner
Listner()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-13-720eb32cc560> in <module>()
7
8
----> 9 Listner()
10
/Users/michalczapski/Bots/BI Bot/Chatfunctions.py in Listner()
12 print("Botty: ",message)
13 return None
---> 14
15 def Listner():
16 with sr.Microphone() as source:
NameError: name 'sr' is not defined
# works
import speech_recognition as sr
r = sr.Recognizer()
def Listner():
with sr.Microphone() as source:
print("...")
audio=r.listen(source)
try:
print("You: "+r.recognize_google(audio));
return r.recognize_google(audio)
except:
pass;
Listner()
解决方案
导入函数中用到的所有模块都需要导入到定义函数的文件中
推荐阅读
- audiokit - Audiokit 鼓合成
- kotlin - 在自定义 Gradle 插件中应用 org.jetbrains.kotlin.jvm 不起作用
- javascript - 如何将 html 表单发布请求翻译成 Postman
- notifications - Microsoft Graph API - CSP 合作伙伴的更改通知
- go - Golang:解释 DumpRequest 和 DumpResponse HTTP/2
- xpath - 其祖先具有包含特定文本字符串的后代的文本节点的 XPath
- python - 将连续数据从 python 脚本流式传输到 NodeJS
- c# - 实例化具有属性的类时的 C# 回调
- json - JSON Schema:如何检查每个具有特定值的数组项是否具有所需的属性?
- python - 提取元素文本