简体   繁体   English

导入模块的返回值不起作用

[英]Return value from imported module not working

I have a python file (project_root/usermodule/user_controls.py) with only the following code: 我有一个仅包含以下代码的python文件(project_root / usermodule / user_controls.py):

class UControl():
    def login(self):
        return 'John'

And I also have a class (project_root/main_file.py) that's importing user_controls (the file above) containing only the following code. 而且我还有一个类(project_root / main_file.py),该类正在导入仅包含以下代码的user_controls(上面的文件)。

import os
from usermodule import user_controls

if __name__ == '__main__':
    uc = user_controls.UControl()
    login_response = uc.login()
    if login_response == 'John':
        print login_response

When I run the second piece of code it gives no error but also no output (when I'm looking for it to output 'John'). 当我运行第二段代码时,它没有错误,但是也没有输出(当我正在寻找输出“ John”的时候)。

Is it a typo in the question or in your program: login_reponse != login_response 是问题还是程序中的错字: login_reponse != login_response

If I correct this error, the script runs flawlessly on my machine. 如果我纠正此错误,则脚本可以在我的计算机上完美运行。 (You may want to use class UControl(object): instead of class UControl(): ). (您可能要使用class UControl(object):而不是class UControl():

The fact that the error is not printed out, suggests that your code isn't even run. 错误没有被打印出来的事实表明您的代码甚至没有运行。 How are you invoking it? 您如何调用它?

Also, the second file, is actually a module (as the first one) and not a class as you are stating in your question. 另外,第二个文件实际上是一个模块(与第一个文件一样),而不是您在问题中指出的类。

Found someone else who was having a similar problem. 找到其他遇到类似问题的人。 I just deleted the file, created a new one, pasted in my old code and compiled/ran it and it worked fine. 我只是删除了文件,创建了一个新文件,将其粘贴到我的旧代码中并对其进行了编译/运行,因此效果很好。 Eclipse playing tricks on me. Eclipse在欺骗我。 Thanks for viewing but this appears now to be solved. 感谢您的收看,但现在看来该问题已解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM