这是什么,为什么应该包括一个 if 声明?
if __name__ == "__main__":
print("Hello, World!")
如果你试图关闭一个问题,有人应该使用这个字符,而不是,考虑关闭作为一个复制为什么Python在我进口时运行我的模块,而我如何停止它? 问题,有人只是没有呼叫任何功能,或者错误地等待一个名为主的功能将自动作为一个输入点,使用为什么没有主()功能运行。
这是什么,为什么应该包括一个 if 声明?
if __name__ == "__main__":
print("Hello, World!")
如果你试图关闭一个问题,有人应该使用这个字符,而不是,考虑关闭作为一个复制为什么Python在我进口时运行我的模块,而我如何停止它? 问题,有人只是没有呼叫任何功能,或者错误地等待一个名为主的功能将自动作为一个输入点,使用为什么没有主()功能运行。
当前回答
在 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__ 不会执行。
其他回答
除了已经提供的信息,如果 __name__ ==“__main__”:技术也是一个很好的方式来确保您的最小和最单一的脚本仍然运行,如果你偶然用Python而不是Python(或Python -m最单一)。
def test_assert():
assert 1 + 2 == 3
if __name__ == "__main__":
import pytest
pytest.main([__file__])
import unittest
class Tests(unittest.TestCase):
def test_assert(self):
self.assertTrue(1 + 2 == 3)
if __name__ == "__main__":
unittest.main()
然后,你的脚本与 Python 呼叫相同,就像 Python -m 最单一的呼叫一样。
现在,如果你也想确保所有的麻烦被转移到问答,如果用Python打电话? 或者,如果你也想添加额外的麻烦? 这里有一个例子如何做到这一点:
def test_assert():
assert 1 + 2 == 3
if __name__ == "__main__":
from pytest import main
from sys import argv
main([*argv, "-s"])
现在,您的 python -v --html=report.html 将具有与 pytest -v --html=report.html 等相同的效果,这是一个很好的方式来确保脚本仍然按照意图运行,即使不与预期的 pytest 或 python -m 最单一的通话运行。
我认为最好在深度和简单的词语中打破答案:
__name__: Python 中的每个模块都有一个特殊的属性,称为 __name__. 它是一个内置变量,返回模块的名称。
__main__:像其他编程语言一样,Python也有一个执行输入点,即主要的“__main__”是顶级代码执行范围的名称,基本上你有两种方式使用Python模块:直接运行它作为脚本,或者进口它。
因此,当模块作为主程序运行时, __name__ 属性的值设置为 __main__. 否则 __name__ 的值设置为包含模块名称。
要简要地说,你需要知道几点:
import a action actually runs all that can be run in a.py, meaning each line in a.py 因为第 1 点,你可能不希望一切都在 a.py 运行,当进口它 解决问题在第 2 点,Python 允许你使用一个条件检查 __name__ 是所有.py 模块中的暗示变量:
当 a.py 进口时, a.py 模块的 __name__ 的值设置为其文件名“a” 当 a.py 直接使用“python a.py” 运行时, __name__ 的值设置为 __main__ 字符串。
然后 python a.py 将运行部分 // 做 A 并进口一个将错过部分 // 做 A
当您的脚本运行时,将其作为命令传递给Python翻译器时,
python myscript.py
定义的函数和类是定义的,好,但它们的代码没有运行。 与其他语言不同,没有主要()函数会自动运行 - 主要()函数是暗示所有代码在顶级。
在这种情况下,顶级代码是如果区块。 __name__ 是一个内置变量,以当前模块的名称进行评估. 但是,如果模块正在直接运行(如 myscript.py 上),则 __name__ 取代设置为“__main__”字符串。
if __name__ == "__main__":
...
如果您的脚本被导入到另一个模块,其各种功能和类定义将被导入,其顶级代码将被执行,但如果上述条款的后代代码不会运行,因为条件不满足。
# file one.py
def func():
print("func() in one.py")
print("top-level in one.py")
if __name__ == "__main__":
print("one.py is being run directly")
else:
print("one.py is being imported into another module")
# file two.py
import one
print("top-level in two.py")
one.func()
if __name__ == "__main__":
print("two.py is being run directly")
else:
print("two.py is being imported into another module")
python one.py
产量将是
top-level in one.py
one.py is being run directly
如果您运行 two.py 而不是:
python two.py
你得到
top-level in one.py
one.py is being imported into another module
top-level in two.py
func() in one.py
two.py is being run directly
当您输入某种具有此条件的代码时,它将返回虚假(进口代码内),但将返回正确的代码将运行。