这是什么,为什么应该包括一个 if 声明?
if __name__ == "__main__":
print("Hello, World!")
如果你试图关闭一个问题,有人应该使用这个字符,而不是,考虑关闭作为一个复制为什么Python在我进口时运行我的模块,而我如何停止它? 问题,有人只是没有呼叫任何功能,或者错误地等待一个名为主的功能将自动作为一个输入点,使用为什么没有主()功能运行。
这是什么,为什么应该包括一个 if 声明?
if __name__ == "__main__":
print("Hello, World!")
如果你试图关闭一个问题,有人应该使用这个字符,而不是,考虑关闭作为一个复制为什么Python在我进口时运行我的模块,而我如何停止它? 问题,有人只是没有呼叫任何功能,或者错误地等待一个名为主的功能将自动作为一个输入点,使用为什么没有主()功能运行。
当前回答
开发和测试您的代码
def do_important():
"""This function does something very important"""
do_important()
以此为主,以此为主,以此为主。
~$ python important.py
问题
但是,如果你想将模块导入到另一个脚本:
import important
# do_important() # I must remember to uncomment to execute this!
更好的方式
if __name__ == "__main__":
do_important()
但是,在这一点上,有一个皮托尼的改善方式。
如果我们想从模块之外运行这个业务过程,怎么办?
如果我们把我们想要练习的代码,当我们开发并测试这样的功能时,然后立即检查“__main__”之后:
def main():
"""business logic for when running this module as the primary one!"""
setup()
foo = do_important()
bar = do_even_more_important(foo)
for baz in bar:
do_super_important(baz)
teardown()
# Here's our payoff idiom!
if __name__ == '__main__':
main()
我们现在有一个最终功能,我们的模块的结束将运行,如果我们运行模块作为主模块。
它将允许模块及其功能和类被进口到其他脚本,而不运行主功能,并将允许模块(及其功能和类)在从不同的“__main__”模块运行时被召唤,即。
import important
important.main()
其他回答
如果 __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__' 。
如果 Python 翻译器运行一个特定的模块,那么 __name__ 全球变量将有“__main__”的值:
def a():
print("a")
def b():
print("b")
if __name__ == "__main__":
print ("you can see me")
a()
else:
print ("You can't see me")
b()
当你运行这个脚本时,它打印:
you can see me
a
如果您输入此文件,请说 A 到 B 文件,然后执行 B 文件,然后如果 __name__ == “__main__” 在 A 文件中变成错误,那么它打印:
You can't see me
b
短答
它是字符串代码,保护用户在他们不打算时偶然提起脚本,这里有一些常见问题,当警卫被忽略了一个脚本:
长答
每当Python翻译器阅读源文件时,它都会做两件事:
让我们看看它是如何工作的,以及它是如何与您的问题有关的 __name__ 检查我们总是看到在 Python 脚本。
让我们使用一个稍微不同的代码样本来探索进口和脚本是如何工作的。
# Suppose this is foo.py.
print("before import")
import math
print("before function_a")
def function_a():
print("Function A")
print("before function_b")
def function_b():
print("Function B {}".format(math.sqrt(100)))
print("before __name__ guard")
if __name__ == '__main__':
function_a()
function_b()
print("after __name__ guard")
当您的模块是主要程序时
python foo.py
# It's as if the interpreter inserts this at the top
# of your module when run as the main program.
__name__ = "__main__"
当您的模块由另一个模块进口时
# Suppose this is in some other main program.
import foo
# It's as if the interpreter inserts this at the top
# of your module when it's imported from another module.
__name__ = "foo"
# Find and load a module given its string name, "math",
# then assign it to a local variable called math.
math = __import__("math")
如果您的模块是主要程序,那么它会看到 __name__ 实际上设置为“__main__”并称为两个功能,打印线“函数 A”和“函数 B 10.0”。
只有当您的模块被另一个模块进口时
总是
它将在两种情况下打印“ __name__ 警卫”字符串。
总结
# What gets printed if foo is the main program
before import
before function_a
before function_b
before __name__ guard
Function A
Function B 10.0
after __name__ guard
# What gets printed if foo is imported as a regular module
before import
before function_a
before function_b
before __name__ guard
after __name__ guard
為什麼它會這樣工作?
# Suppose this is foo2.py.
import os, sys; sys.path.insert(0, os.path.dirname(__file__)) # needed for some interpreters
def function_a():
print("a1")
from foo2 import function_b
print("a2")
function_b()
print("a3")
def function_b():
print("b")
print("t1")
if __name__ == "__main__":
print("m1")
function_a()
print("m2")
print("t2")
# Suppose this is foo3.py.
import os, sys; sys.path.insert(0, os.path.dirname(__file__)) # needed for some interpreters
def function_a():
print("a1")
from foo3 import function_b
print("a2")
function_b()
print("a3")
def function_b():
print("b")
print("t1")
print("m1")
function_a()
print("m2")
print("t2")
# Suppose this is in foo4.py
__name__ = "__main__"
def bar():
print("bar")
print("before __name__ guard")
if __name__ == "__main__":
bar()
print("after __name__ guard")
例如,这是 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
创建以下两个文件:
# a.py
import b
# b.py
print("__name__ equals " + __name__)
if __name__ == '__main__':
print("if-statement was executed")
现在将每个文件单独运行。
使用 Python A.Py:
$ python a.py
__name__ equals b
当 a.py 运行时,它会导入 b 模块,这会导致 b 内部的所有代码运行。
运行Python b.py:
$ python b.py
__name__ equals __main__
if-statement was executed
当只执行 b.py 文件时,Python 在此文件中将 globals()['__name__'] 设置为 "__main__". 因此,如果声明评估为 True 这一次。