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

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

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


当前回答

所有答案都解释了功能性,但我会给出一个例子它的使用,这可能有助于进一步清理这个概念。

假设你有两个 Python 文件, a.py 和 b.py. 现在, a.py 输入 b.py. 我们运行 a.py 文件,其中输入 b.py 代码首先执行。

在 b.py 代码中,有一些代码是专属于 b.py 文件,我们不希望任何其他文件(除了 b.py 文件),已进口了 b.py 文件,运行它。

如果它是主要文件(即,b.py)运行代码,在这种情况下它不是(a.py是主要文件运行),那么只有代码被执行。

其他回答

您可以将文件作为一个脚本以及一个可进口的模块使用。

fibo.py(一个名为fibo的模块)

# Other modules can IMPORT this MODULE to use the function fib
def fib(n):    # write Fibonacci series up to n
    a, b = 0, 1
    while b < n:
        print(b, end=' ')
        a, b = b, a+b
    print()

# This allows the file to be used as a SCRIPT
if __name__ == "__main__":
    import sys
    fib(int(sys.argv[1]))

参考: https://docs.python.org/3.5/tutorial/modules.html

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


开发和测试您的代码

def do_important():
    """This function does something very important"""

do_important()

以此为主,以此为主,以此为主。

~$ python important.py

问题

但是,如果你想将模块导入到另一个脚本:

import important

# do_important() # I must remember to uncomment to execute this!

更好的方式

if __name__ == "__main__":
    do_important()

但是,在这一点上,有一个皮托尼的改善方式。

如果我们想从模块之外运行这个业务过程,怎么办?

如果我们把我们想要练习的代码,当我们开发并测试这样的功能时,然后立即检查“__main__”之后:

def main():
    """business logic for when running this module as the primary one!"""
    setup()
    foo = do_important()
    bar = do_even_more_important(foo)
    for baz in bar:
        do_super_important(baz)
    teardown()

# Here's our payoff idiom!
if __name__ == '__main__':
    main()

我们现在有一个最终功能,我们的模块的结束将运行,如果我们运行模块作为主模块。

它将允许模块及其功能和类被进口到其他脚本,而不运行主功能,并将允许模块(及其功能和类)在从不同的“__main__”模块运行时被召唤,即。

import important
important.main()

创建一个文件, a.py:

print(__name__) # It will print out __main__

__name__ 总是相当于 __main__ 每当此文件运行时,直接显示此文件是主要文件。

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

import a  # Prints a

它将打印一个,即所进口的文件的名称。

因此,要显示相同文件的两个不同的行为,这是一个常用的技巧:

# Code to be run when imported into another python file

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

考虑一下:

print __name__

上面的结果是 __main__。

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

上述声明是真实的,并打印“直接方法”。假设如果他们进口这个类别到另一个类别,它不会打印“直接方法”,因为在进口时,它将设置 __name__ 等于“第一型号名称”。