这是什么,为什么应该包括一个 if 声明?

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

如果你试图关闭一个问题,有人应该使用这个字符,而不是,考虑关闭作为一个复制为什么Python在我进口时运行我的模块,而我如何停止它? 问题,有人只是没有呼叫任何功能,或者错误地等待一个名为主的功能将自动作为一个输入点,使用为什么没有主()功能运行。


当前回答

如果 __name__ ==“__main__”:该怎么办?

__name__ 是一个全球变量(在 Python 中,全球实际上意味着在模块层面)存在于所有名称空间。

作为唯一的特殊情况,但是,在任何Python过程你运行,如 mycode.py:

python mycode.py

否则匿名的全球名称空间将“__main__”的值归咎于其 __name__。

因此,包括最后的线条

if __name__ == '__main__':
    main()

在您的 mycode.py 脚本的结尾,当它是由 Python 过程运行的主要输入点模块时,

将使您的脚本独特定义的主要功能运行。

使用此构造的另一个好处:您也可以将代码作为一个模块进口到另一个脚本,然后运行主功能,如果和当您的程序决定:

import mycode
# ... any amount of other code
mycode.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__' 。

除了已经提供的信息,如果 __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 最单一的通话运行。

这个答案是Java程序员学习Python. 每个Java文件通常包含一个公共类。

从其他文件中呼叫课堂. 你只需要将其进口到呼叫程序. 单独运行课堂站,用于测试目的。

对于后者来说,该类应该包含一个公共静态虚假主要()方法,在Python中,这个目的由全球定义的标签“__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

简单地说,这是运行文件的输入点,就像C编程语言的主要功能一样。