这是什么,为什么应该包括一个 if 声明?
if __name__ == "__main__":
print("Hello, World!")
如果你试图关闭一个问题,有人应该使用这个字符,而不是,考虑关闭作为一个复制为什么Python在我进口时运行我的模块,而我如何停止它? 问题,有人只是没有呼叫任何功能,或者错误地等待一个名为主的功能将自动作为一个输入点,使用为什么没有主()功能运行。
这是什么,为什么应该包括一个 if 声明?
if __name__ == "__main__":
print("Hello, World!")
如果你试图关闭一个问题,有人应该使用这个字符,而不是,考虑关闭作为一个复制为什么Python在我进口时运行我的模块,而我如何停止它? 问题,有人只是没有呼叫任何功能,或者错误地等待一个名为主的功能将自动作为一个输入点,使用为什么没有主()功能运行。
当前回答
接收“ab.py”文件:
def a():
print('A function in ab file');
a()
import ab
def main():
print('main function: this is where the action is')
def x():
print ('peripheral task: might be useful in other projects')
x()
if __name__ == "__main__":
main()
当您执行 xy.py 时,您将导入 ab. 导入声明立即在导入上运行模块,因此 ab 的操作在 xy 的剩余之前进行。
当你运行一个脚本 - 无论你命名什么 - 翻译者称之为“__main__”,使它成为主或“家”脚本,在运行一个外部脚本后返回。
下面的两行意味着:“如果这是“__main__”或“home”脚本,执行称为main()的函数”。这就是为什么你会看到一个 def main():锁上顶部,其中包含脚本的功能的主要流。
為什麼要實施這一點?
但代码在没有它的情况下工作。
其他回答
您可以通过这个简单的例子查看特殊变量 __name__:
创建 file1.py
if __name__ == "__main__":
print("file1 is being run directly")
else:
print("file1 is being imported")
创建 *file2.py
import file1 as f1
print("__name__ from file1: {}".format(f1.__name__))
print("__name__ from file2: {}".format(__name__))
if __name__ == "__main__":
print("file2 is being run directly")
else:
print("file2 is being imported")
执行 file2.py
出口:
file1 is being imported
__name__ from file1: file1
__name__ from file2: __main__
file2 is being run directly
如果 __name__ == '__main__':下面的代码将仅执行,如果模块被称为脚本。
作为一个例子,请考虑以下模块 my_test_module.py:
# my_test_module.py
print('This is going to be printed out, no matter what')
if __name__ == '__main__':
print('This is going to be printed out, only if user invokes the module as a script')
第一個選項:將 my_test_module.py 輸入到另一個模組
# main.py
import my_test_module
if __name__ == '__main__':
print('Hello from main.py')
现在,如果你引用 main.py:
python main.py
>> 'This is going to be printed out, no matter what'
>> 'Hello from main.py'
请注意,仅在 my_test_module 中执行顶级印刷() 声明。
第二种可能性:将 my_test_module.py 作为脚本提交
现在,如果你将 my_test_module.py 作为 Python 脚本运行,那么打印() 陈述将运行:
python my_test_module.py
>>> 'This is going to be printed out, no matter what'
>>> 'This is going to be printed out, only if user invokes the module as a script'
对于更全面的解释,您可以阅读在 Python 中做什么 __name__ == '__main__' 。
简单的说法:
下面看到的代码如果 __name__ ==“__main__”:只有当您的 Python 文件作为 python 示例1.py 执行时才会被召回。
但是,如果您想将您的 Python 文件 example1.py 作为一个模块来与另一个 Python 文件一起工作,请说 example2.py,下面的代码如果 __name__ ==“__main__”:不会运行或采取任何效果。
在 Python 中,每个模块都有一个称为 __name__ 的属性. __name__ 的值是 __main__ 当模块直接运行时,如 python my_module.py. 否则(如您说 import my_module) __name__ 的值是模块的名称。
一个小例子要简要解释。
脚本测试.py
apple = 42
def hello_world():
print("I am inside hello_world")
if __name__ == "__main__":
print("Value of __name__ is: ", __name__)
print("Going to call hello_world")
hello_world()
我们可以直接这样做。
python test.py
出口
Value of __name__ is: __main__
Going to call hello_world
I am inside hello_world
现在假设我们从另一个脚本中称之为上面的脚本:
编辑 external_calling.py
import test
print(test.apple)
test.hello_world()
print(test.__name__)
当你执行这一点时,
python external_calling.py
出口
42
I am inside hello_world
test
因此,上述是自我解释的,当你从另一个脚本打电话测试时,如果在 test.py 中的 loop __name__ 不会执行。
例如,这是 test1.py 没有如果 __name__ ==“__main__”::
# "test1.py"
def hello()
print("Hello")
hello()
而且 test2.py 只輸入 test1.py:
# "test2.py"
import test1 # Here
然后,当运行 test2.py 时,一个 Hello 被打印,因为 test1.py 中的不需要的 hello() 代码也被运行:
python test2.py
Hello
当然,您可以在 test2.py 中拨打 test1.hello():
# "test2.py"
import test1
test1.hello() # Here
然后,当运行测试2时,印刷了两个Hello:
python test2.py
Hello
Hello
现在,添加如果 __name__ ==“__main__”:测试1.py 并将 hello() 放在下面:
# "test1.py"
def hello()
print("Hello")
if __name__ == "__main__":
hello()
此分類上一篇: Test2.py:
# "test2.py"
import test1
test1.hello()
然后,在运行 test2.py 时,只有一个 Hello 被打印,因为如果 __name__ ==“__main__”:在 test2.py 输入 test1.py 时阻止运行不需要的 hello() 代码:
python test2.py
Hello
此外, test1.py 是否有 __name__ == “__main__”: 或者不:
# "test1.py"
def hello()
print("Hello")
if __name__ == "__main__":
hello()
# "test1.py"
def hello()
print("Hello")
hello()
One Hello 在运行 test1.py 时正确打印:
python test1.py
Hello