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

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

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


当前回答

创建文件,a.py , a.py , a.py , a.py , a.py , a.py , a.py , a.py:

print(__name__) # It will print out __main__

__name__始终等于__main__当该文件为直接直接运行显示此为主文件 。

创建另一个文件,b.py b.py,在同一目录中:

import a  # Prints a

运行它,它会打印a,即哪个文件的名称已导入.

所以,为了展示同一文件的两种不同行为,这是一个常用的把戏:

# Code to be run when imported into another python file

if __name__ == '__main__':
    # Code to be run only when run directly

其他回答

除了已经提供的资料外,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__":无法在导入文件时运行不需要的代码 。

例如,这是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__":是吗?

__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()

主要是为了避免导入锁定可能产生的问题引起的问题直接导入代码. 你想要main()如果您的文件被直接引用( 即__name__ == "__main__"如果您的代码是导入的, 则进口者必须从真正的主模块输入您的代码, 以避免进口锁出现问题 。

一个副作用是,您自动签署一个支持多个切入点的方法。您可以使用main()作为切入点,但你没有 不得不时段setup.py预计预计预计预计main(),其他工具使用其他切入点。例如,将文件作为gunicorn进程,您定义app()函数代替main()。 和setup.py, gunicorn输入密码时不要做任何事情(因为进口锁问题)。

缩略Python 主函数是一个程序的起始点。当程序运行时, Python 口译员会按顺序运行代码。主要函数只有在作为 Python 程序运行时才执行...

def main():
    print("I am in the function")

print("I am out of the function")

当运行脚本显示时 :

I am out of the function

而不是代码"我是在函数"。

这是因为我们没有宣布调用功能“if_name”主要".

使用时使用:

def main():
    print("I am in the function")

if __name__ == "__main__":
    main()

print("I am out of the function")

输出等于

I am in the function
I am out of the function

在皮顿if__name__== "__main__"允许您作为可重复使用的模块或独立程序运行 Python 文件。

当 Python 口译员读取源文件时, 它会执行其中的所有代码。 当 Python 运行“ 源文件” 作为主程序时, 它会设置特殊变量__name__要有一个值"__main__".

当您执行主函数时,它会读取if用来检查__name__等于__main__.