[英]Error with importing self defined function
When I try to import a function from a file, it doesn't recognize the module that I imported earlier, while when its defined directly then there is no problem. 当我尝试从文件导入函数时,它无法识别我之前导入的模块,而当它直接定义时则没有问题。
The module is imported as sr. 该模块作为sr导入。 Should I import it body of the function or is there any other trick.
我应该导入它的功能主体还是有任何其他技巧。
# 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()
导入函数中使用的所有模块都需要导入到定义函数的文件中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.