这样做做什么,为什么应该包括:if语句?

if __name__ == "__main__":
    print("Hello, World!")

如果你试图结束一个问题 如果你想要结束一个问题 在那里有人应该应该 使用这个学说,而不是,是不是, 考虑关闭作为 重复的为什么Python在我进口时 运行我的模块? 我该如何阻止它?。而对于某些问题,如果有人只是没有调用任何函数,或者错误地期望指定函数main自动用作切入点,使用当我启动 Python 脚本时, 主函数为何不运行? 脚本从哪里开始运行 ?.


当前回答

除了已经提供的资料外,if __name__ == "__main__":技术也是确保pytestunittest如果您不小心调用脚本, 脚本仍然在运行python代替pytest(或)python -m unittest举例来说:

def test_assert():
    assert 1 + 2 == 3

if __name__ == "__main__":
    import pytest
    pytest.main([__file__])

现在不管怎么称呼你的测试 都会在测试中进行pytestpython。在这里,这是unittest版本 :

import unittest

class Tests(unittest.TestCase):
    def test_assert(self):
        self.assertTrue(1 + 2 == 3)

if __name__ == "__main__":
    unittest.main()

然后您的脚本用python以一计号,以python -m unittest拨打。

现在,如果你也想确保 你所有的价码都通过pytest如果被召唤时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,等等。这是一个很好的方法,可以确保脚本仍然照原意运行, 即使没有按照预期运行pytestpython -m unittest电话

其他回答

简单的答案就是代码 代码是写在如果名称名称名称名称名称 == "主要":如果您在其它文件中导入的话, 将不会执行 。

当您的脚本作为命令传递给 Python 口译员时,

python myscript.py

缩进级别 0 的所有代码都得到执行。 定义的函数和类别是, 定义的, 定义明确, 但他们的代码没有运行 。 与其他语言不同, 没有main()函数自动运行 -main()函数是最高层所有代码的隐含值。

在这种情况下,最高层代码是if区块。__name__是一个内嵌变量。该变量对当前模块的名称进行评估。但是,如果模块直接运行,则该模块直接运行(如myscript.py above), then __name__而不是设置为字符串"__main__"因此,您可以测试您的脚本是直接运行的,还是通过测试输入其它东西的脚本。

if __name__ == "__main__":
    ...

如果您的脚本正在导入到另一个模块中, 它的各种函数和类定义将会导入, 并且它的顶层代码将会被执行, 但是在当时if以上条款将无法运行,因为不符合条件。作为一个基本例子,考虑以下两个脚本:

# 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

因此,当模块one装上子弹,装上子弹__name__等于相等"one"代替"__main__".

简单的说,它是一个切入点 运行文件,像main函数中C编程语言。

if __name__ == "__main__":无法在导入文件时运行不需要的代码 。

例如,这是test1.pyif __name__ == "__main__"::

# "test1.py"

def hello()
    print("Hello")

hello()

还有test2.py进口test1.py:

# "test2.py"

import test1 # Here


然后,当奔跑的时候,test2.py, 1个Hello打印是因为不需要的代码hello()test1.py同时运行 :

python test2.py
Hello

当然,你可以打给test1.hello()test2.py:

# "test2.py"

import test1

test1.hello() # Here

然后,当奔跑的时候,test2, Hello已经打印 :

python test2.py
Hello
Hello

现在,添加if __name__ == "__main__":test1.py并放hello()在其之下:

# "test1.py"

def hello()
    print("Hello")

if __name__ == "__main__":
    hello()

这是test2.py:

# "test2.py"

import test1

test1.hello()

然后,当奔跑的时候,test2.py, 只有一个Hello打印是因为if __name__ == "__main__":防止要运行不需要的代码hello()何时test2.py进口进口进口test1.py:

python test2.py
Hello

此外,是否test1.py拥有if __name__ == "__main__":或非:

# "test1.py"

def hello()
    print("Hello")

if __name__ == "__main__":
    hello()
# "test1.py"

def hello()
    print("Hello")

hello()

一个Hello运行时正确打印test1.py:

python test1.py
Hello

简而言之,你需要知道几点:

  1. import a动作实际上运行所有可以运行的a.py,意指每一行a.py

  2. 因为第1点,你可能不希望一切 运行在a.py进口时

  3. 要解决第2点的问题, Python 允许您使用条件检查

  4. __name__全部为隐含变量.py模块 :

  • 何时a.pyimported, 数值__name__联 联 年 月 日 月 日 月 月 日 月 月 日 月 月 月 日 月 月 日 月 月 日 月 月 月 日 月 月 日 月 月 月 日 月 的 月 月 月 日 月 月 日 月 的 月 月 月 月 日 月 月 月a.py模块设置为文件名 "a"
  • 何时a.py直接使用“python a.py" , " 价值__name__设置为字符串__main__
  1. 基于 Python 如何设置变量的机制__name__每个模块的每个模块, 您知道如何达到第3点吗 ? 答案相当简单, 对吧? 使用如果条件 :if __name__ == "__main__": // do A
  • 时当时python a.py将运行此部件// do A
  • import a跳过此部分// do A
  1. 你甚至可以放__name__ == "a"取决于你的功能需要,但很少

Python在4点是特别的 重要的事!

我在这页的答案中读了这么多。我想说,如果你知道这件事的话,你肯定能理解那些答案, 否则,你还是很困惑。