简体   繁体   English

导入自定义函数时出错

[英]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.

相关问题 导入自定义包和模块时遇到问题? - Problems in importing self-defined packages and modules? Python错误-未定义“自我” - Python Error — “self” is not defined 自定义函数的未知误差,用于逼近积分 - Unknown error with self-defined function for approximation of an integral 为什么函数参数之外的“self”会给出“未定义”的错误? - Why does “self” outside a function's parameters give a “not defined” error? 带有自定义 function 的 SHAP - SHAP with a self-defined function 在定义了 function 之后导入一个模块 - Importing a module after a function is defined 为什么会出现self未定义的错误? - why is the error of self not defined occuring? 得到一个错误,说 &#39;self.mydictValues&#39; is not defined,只得到该函数中的错误(在其他人中使用并且完美运行) - Get an error saying 'self.mydictValues' is not defined, only get the error in that function (used in others and works perfectly) 带有自定义损失函数的 TensorFlow 2 的无效参数错误(学生 t 分布) - Invalid argument error with TensorFlow 2 with self-defined loss function (Student t distribution) 带有自定义损失函数的 TensorFlow 2 的无效参数错误,尽管一切似乎都是正确的 - Invalid argument error with TensorFlow 2 with self-defined loss function, although everything seems to be correct
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM