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

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

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


当前回答

如果 __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__' 。

其他回答

系统(Python 翻译器)为源文件(模块)提供的一些变量,您可以随时获得它们的值,所以,让我们专注于 __name__ 变量/属性:

如果 Python 将此源代码文件作为主程序(即您运行的文件),则将此文件的特殊 __name__ 变量设置为“__main__”值。

如果从另一个模块进口,则 __name__ 将设置到该模块的名称。

所以,在你的例子中,部分:

if __name__ == "__main__":
   lock = thread.allocate_lock()
   thread.start_new_thread(myfunction, ("Thread #: 1", 2, lock))
   thread.start_new_thread(myfunction, ("Thread #: 2", 2, lock))

这意味着代码区块:

lock = thread.allocate_lock()
thread.start_new_thread(myfunction, ("Thread #: 1", 2, lock))
thread.start_new_thread(myfunction, ("Thread #: 2", 2, lock))

希望这能帮出来。

原因为

if __name__ == "__main__":
    main()

首先要避免进口锁问题,即将直接输入代码,您想要主()运行,如果您的文件被直接提交(这是 __name__ ==“__main__”案例),但如果您的代码被进口,则进口商必须从真正的主要模块输入代码,以避免进口锁问题。

一个副作用是,你自动登录到一个方法,支持多个输入点. 你可以运行你的程序使用主要()作为输入点,但你不需要. 虽然 setup.py 等待主要(),其他工具使用替代输入点. 例如,运行你的文件作为一个 gunicorn 过程,你定义一个 app() 函数而不是一个主要()。

例如,这是 test1.py 没有如果 __name__ ==“__main__”::

# "test1.py"

def hello()
    print("Hello")

hello()

而且 test2.py 只輸入 test1.py:

# "test2.py"

import test1 # Here


然后,当运行 test2.py 时,一个 Hello 被打印,因为 test1.py 中的不需要的 hello() 代码也被运行:

python test2.py
Hello

当然,您可以在 test2.py 中拨打 test1.hello():

# "test2.py"

import test1

test1.hello() # Here

然后,当运行测试2时,印刷了两个Hello:

python test2.py
Hello
Hello

现在,添加如果 __name__ ==“__main__”:测试1.py 并将 hello() 放在下面:

# "test1.py"

def hello()
    print("Hello")

if __name__ == "__main__":
    hello()

此分類上一篇: Test2.py:

# "test2.py"

import test1

test1.hello()

然后,在运行 test2.py 时,只有一个 Hello 被打印,因为如果 __name__ ==“__main__”:在 test2.py 输入 test1.py 时阻止运行不需要的 hello() 代码:

python test2.py
Hello

此外, test1.py 是否有 __name__ == “__main__”: 或者不:

# "test1.py"

def hello()
    print("Hello")

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

def hello()
    print("Hello")

hello()

One Hello 在运行 test1.py 时正确打印:

python test1.py
Hello

创建以下两个文件:

# a.py

import b
# b.py

print("__name__ equals " + __name__)

if __name__ == '__main__':
    print("if-statement was executed")

现在将每个文件单独运行。


使用 Python A.Py:

$ python a.py
__name__ equals b

当 a.py 运行时,它会导入 b 模块,这会导致 b 内部的所有代码运行。


运行Python b.py:

$ python b.py
__name__ equals __main__
if-statement was executed

当只执行 b.py 文件时,Python 在此文件中将 globals()['__name__'] 设置为 "__main__". 因此,如果声明评估为 True 这一次。

如果 __name__ ==“__main__”是当脚本从(说)命令线运行时运行的部分,使用像 python myscript.py 这样的命令。