导入自定义函数时出错

时间:2019-05-31 22:19:37

标签: python import

当我尝试从文件中导入函数时,它无法识别我之前导入的模块,而直接对其进行定义时就没有问题。

该模块作为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()

1 个答案:

答案 0 :(得分:0)

导入功能中使用的所有模块都必须导入定义功能的文件中