如果导入模块,为什么什么也没运行? [英] Why is nothing running if I import a module?
问题描述
我正在导入一个模块,但是当我运行它时,它说进程以退出代码0完成
I'm importing a module but when I run it, it says Process finished with exit code 0
我的代码:来自游戏导入*
我已经尝试了所有建议的方法,甚至查看了堆栈溢出的答案,但是没有任何效果.
I've tried everything that's been suggested and I even looked at answers from stack overflow but nothing is working.
该模块在运行时可以正常运行,但是我不知道为什么它不起作用.如果您有任何建议,请让我知道.谢谢.
The module works fine when I run it but I have no idea on why it isn't working. If you have any suggestions please let me know. Thank you.
推荐答案
if __name__ =='main'
块的全部目的是在导入该脚本时阻止其内部的代码运行作为一个模块;仅主要"您直接执行的脚本将执行该语句中的代码.如果只用直接调用 main()
代替该语句,则该函数将在导入时运行.
The whole point of the if __name__ == 'main'
block is to stop the code inside it from running when importing that script as a module; only the "main" script, the one you're directly executing, will execute the code within that statement. If you just replace that statement with a direct call to main()
, then that function will run upon import.
有关更多信息,请参见: __name__ =="__main__"怎么办:
For more info, see: What does if __name__ == "__main__": do?
如果您想从脚本中执行 Game
的 main
,则您的代码必须类似于:
If you want to execute Game
's main
from your script, your code would have to look like:
from Game import *
main()
这篇关于如果导入模块,为什么什么也没运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!