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

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

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


当前回答

什么是什么东西if __name__ == "__main__":是吗?

__name__是一个全球变量(在 Python 中,在模块模块级别)存在于所有命名空间的所有命名空间中。它通常是模块的名称(作为str类型))

然而,作为唯一的特殊情况,无论你运行的 Python 程序,就像我的代码。 py:

python mycode.py

以其他匿名的全域命名空间被指定为'__main__'至其__name__.

因此,包括最后一行

if __name__ == '__main__':
    main()
  • 在我的代码.py脚本的结尾,
  • 当它是由 Python 进程运行的初级切入点模块时,

将会导致您的脚本有独特的定义main要运行的函数。

使用此构造的另一个好处是:您也可以将代码作为模块导入到另一个脚本中,然后运行主函数,如果且当您的程序决定:

import mycode
# ... any amount of other code
mycode.main()

其他回答

if __name__ == "__main__":基本上是顶层的脚本环境, 它指定了翻译( “ 我最优先首先执行 ” ) 。

'__main__'是顶层代码执行范围的名称。模块的__name__设置为等于'__main__'从标准输入、脚本或交互式提示读取。

if __name__ == "__main__":
    # Execute only if run as a script
    main()

考虑:

print __name__

上述产出是__main__.

if __name__ == "__main__":
  print "direct method"

上述陈述属实,印有指纹“直接方法”假想他们是否将这门课 输入到另一门课上,它不会打印“直接方法”因为在进口时,它将设置__name__ equal to "first model name".

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

除了已经提供的资料外,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电话

简言之:

您所看到的代码下面的代码if __name__ == "__main__":只有当您的 Python 文件被执行时才会被调用 。python example1.py

但是,如果您想要导入您的 Python 文件example1.py以模块的形式使用另一个 Python 文件, 例如example2.py下的代码if __name__ == "__main__":将不会运行或产生任何效果。