Python: if __name__ == "__main__"

来源:互联网 发布:网络电视台广告语 编辑:程序博客网 时间:2024/05/07 15:51

http://luy.li/2010/05/31/python__name__main__/

为什么python里要 if __name__ == ‘__main__’:

尽管python允许你像shell脚本一样,把大段的代码堆积着写,但是,很多python入门的书,都会建议你把代码写成一个函数,然后在最后面统一调用,例如这样:

def main():    #具体代码 if __name__ == '__main__':    main()

很多文章都会说这个是由于代码风格之类的原因,但是其实,不这样写,有时候也是会直接导致出错的。
举个例子,打开《A Byte of Python》的类变量那节,可以这里看在线版本,然后把中间的那示例代码复制下来,运行,可以看到,确实能得到预期正确的结果。
但是,现在把那代码里的所有“swaroop”换成“xxx1”,再把所有“kalam”换成“yyy1”,再运行,就会在程序运行的最后时刻,得到这么一个奇怪的错误(Python 2.6.5下实验):
Exception AttributeError: "'NoneType' object has no attribute 'population'" in <bound method="" person.__del__="" of="" <__main__ person="" instance="" at="" 0xb689090c="">> ignored
也就是说,换了个变量名,程序就出错了。。。
究其原因,应该是python在最后析构所有类和对象的时候,并没有处理这些对象之间的依赖关系,而是根据变量名来决定某种顺序操作了。从而导致Person类本身,先于yyy2被干掉了,所以在解构yyy2的时候,执行它的__del__()方法,却发现父类都已经没有了~~
这应该确实算是python解析器的bug,但是,如果那把这个例子里的所有class Person以外的代码,写到一个main()函数里,再通过上述方法调用的话,就不会发生这种错误了。
所以,大家最好还是别太在意多几行代码,养成这个好习惯吧。




http://f.dataguru.cn/thread-186411-1-1.html

__name__是指示当前py文件调用方式的方法。如果它等于"__main__"就表示是直接执行,如果不是,则用来被别的文件调用,这个时候if就为False,那么它就不会执行最外层的代码了。
比如你有个Python文件里面
def XXXX():
    #body
print "asdf"
这样的话,就算是别的地方导入这个文件,要调用这个XXXX函数,也会执行print "asdf",因为他是最外层代码,或者叫做全局代码。但是往往我们希望只有我在执行这个文件的时候才运行一些代码,不是的话(也就是被调用的话)那就不执行这些代码,所以一般改为
def XXXX():
     #body
if __name__="__main__":
     print "asdf"


http://keliang.blog.51cto.com/3359430/649318

想必很多初次接触python的同学都会见到这样一个语句,if __name__ == "__main__":

那么这个语句到底是做什么用的呢?在解释之前,首先要声明的是,不管你是多么小白,你一定要知道的是:

1.python文件的后缀为.py;

2..py文件既可以用来直接执行,就像一个小程序一样,也可以用来作为模块被导入(比如360安全卫士,就是依靠一个个功能模块来实现的,好比360安全卫士本身框架是一个桌面,而上面的图标就是快捷方式,这些快捷方式所指向的就是这一个个功能模块)

3.在python中导入模块一般使用的是import

好了,在确定知道以上几点之后,就可以开始解释if __name__ == "__main__":这个语句了。

首先解释一下if,顾名思义,if就是如果的意思,在句子开始处加上if,就说明,这个句子是一个条件语句。学习if语句的使用是很简单的,当然想要真正灵活运用还需大量的实践。

接着是 __name__,__name__作为模块的内置属性,简单点说呢,就是.py文件的调用方式。

最后是__main__,刚才我也提过,.py文件有两种使用方式:作为模块被调用和直接使用。如果它等于"__main__"就表示是直接执行。

总结:在if __name__ == "__main__":之后的语句作为模块被调用的时候,语句之后的代码不执行;直接使用的时候,语句之后的代码执行。通常,此语句用于模块测试中使用。


http://blog.sina.com.cn/s/blog_605f5b4f0101c96w.html

答案源于:http://stackoverflow.com/questions/419163/what-does-if-name-main-do,example是自己写的。


When the Python interpreter reads a source file, it executes all of the code found in it. Before executing the code, it will define a few special variables. For example, if the python interpreter is running that module (the source file) as the main program, it sets the special __name__ variable to have a value"__main__".
 If this file is being imported from another module, __name__ will be set to the module's name.

举例说明:
#one.py
1 def func():
  2     print("func() in one.py")
  3 
  4 print("top-level in one.py")
  5 print("one.py module name :%s"%(__name__))
  6 if __name__ == "__main__":
  7     print("one.py is being run directly")
  8 else:
  9     print("one.py is being imported into another module")

#two.py
 1 import one
  2 
  3 print("top-level in two.py")
  4 one.func()
  5 
  6 if __name__ == "__main__":
  7     print("two.py is being run directly")
  8 else:
  9     print("two.py is being imported into another module")

python one.py的结果:
top-level in one.py
one.py module name :__main__
one.py is being run directly

python two.py的结果:
top-level in one.py
one.py module name :one
one.py is being imported into another module #加载one的时候由于one也是可执行的,所以就会执行one,导致前三行的输出
top-level in two.py
func() in one.py
two.py is being run directly

交换two.py中的第一行import和第三行的输出,重新执行python two.py 的结果:
top-level in two.py
top-level in one.py # one.py的输出对应于其在two.py中import one 的位置
one.py module name :one
one.py is being imported into another module
func() in one.py
two.py is being run directly

http://www.crifan.com/python_detailed_explain_about___name___and___main__/

【整理】Python中的__name__和__main__含义详解

背景

在写Python代码和看Python代码时,我们常常可以看到这样的代码:

?
1
2
3
4
5
def main():
    ......
 
if __name == "__main__":
    main();

其中的函数名main,也可以是其他任意的,你所设置的名字。

这里,就出现了,我们此处所要解释的

__name__和__main__

 

__name__和__main的含义

其实,对于Python中的这类问题,根据我之前的:

【整理】如何学习Python + 如何有效利用Python有关的网络资源 + 如何利用Python自带手册(Python Manual)

中的介绍,最好的学习的方法,其实就是去看官网文档:

__name__的含义

__name__ is set to name of module

另外还有几处的解释:

Modules…

Predefined (writable) attributes: __name__ is the module’s name;

… Classes…

Special attributes: __name__ is the class name;

从这几处的解释,我们很容易理解其基本的意思:

__name__,

如果是放在Modules模块中,就表示是模块的名字;

如果是放在Classs类中,就表示类的名字;

 

__main__的含义

同理,先去官网文档中看对应的解释:

__main__ be inited

对应的其他几处的解释是:

4.1. Naming and binding

The namespace for a module is automatically created the first time a module is imported. The main module for a script is always called __main__.

8.1. Complete Python programs

While a language specification need not prescribe how the language interpreter is invoked, it is useful to have a notion of a complete Python program. A complete Python program is executed in a minimally initialized environment: all built-in and standard modules are available, but none have been initialized, except for sys (various system services), __builtin__ (built-in functions, exceptions and None) and __main__. The latter is used to provide the local and global namespace for execution of the complete program.

….

The interpreter may also be invoked in interactive mode; in this case, it does not read and execute a complete program but reads and executes one statement (possibly compound) at a time. The initial environment is identical to that of a complete program; each statement is executed in the namespace of __main__.

 

void Py_Initialize()

Initialize the Python interpreter. In an application embedding Python, this should be called before using any other Python/C API functions; with the exception of Py_SetProgramName(), PyEval_InitThreads(), PyEval_ReleaseLock(), and PyEval_AcquireLock(). This initializes the table of loaded modules (sys.modules), and creates the fundamental modules __builtin__, __main__ and sys.

说实话,对于__main__,上述的解释,并不是那么容易看的懂。

其实要看懂上面的解释,我们首先要知道的一些前提是:

python代码,是可以直接一行行写出来,然后去运行,是可以的。但是这只是针对我们的小程序来说的。

更多的Python代码,是写成更加通用的,可以被调用的,可以重复利用的,模块的形式;

所以都是写在对应的函数里面的。

而作为模块,就是上面的解释中的:

The namespace for a module is automatically created the first time a module is imported. The main module for a script is always called __main__.

模块第一次被导出(import)后,系统会自动为其创建一个域名空间(namespace);

(模块,都是有自己的名字的)此处的脚本的主模块的名字,始终都叫做__main__。

 

用代码来演示__name__和__main__的含义和用法

示例1

文字解释,还是很容易糊涂的,下面就来借用一个这里:

What is ‘if __name__ == “__main__”‘ for?

的代码来详细解释一下。

 

作为普通的代码,我们是可以这么写的:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
【整理】Python中的__name__和__main__含义详解
 
http://www.crifan.com/python_detailed_explain_about___name___and___main__
 
Version:    2012-11-17
Author:     Crifan
Contact:    http://www.crifan.com/contact_me/
"""
 
def square(x):
    return * x
 
print "test: square(42) ==",square(42);

对应的,运行结果也是很正常的:

?
1
2
E:\Dev_Root\python\__name___and___main__>__name___and___main__.py
test: square(42) == 1764

但是呢,往往我们所见到的,和我们以后自己也会遇到的,自己写的,有条理的,可复用的做法,那肯定是

那对应的square等函数,专门放到一个文件中,然后被别人调用,此时,就是这样的:

对应的模块文件是mymath.py,里面有我们实现的函数square:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
【整理】Python中的__name__和__main__含义详解
 
http://www.crifan.com/python_detailed_explain_about___name___and___main__
 
Version:    2012-11-17
Author:     Crifan
Contact:    http://www.crifan.com/contact_me/
"""
 
def square(x):
    return * x
 
print "test: square(42) ==",square(42);

 

然后别的python文件__name___and___main__.py中,导入此mymath模块,然后使用其square函数:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
【整理】Python中的__name__和__main__含义详解
 
http://www.crifan.com/python_detailed_explain_about___name___and___main__
 
Version:    2012-11-17
Author:     Crifan
Contact:    http://www.crifan.com/contact_me/
"""
 
import mymath;
 
print "In caller, test for mymath: square(12)=",mymath.square(12);

然后运行结果是:

?
1
2
3
E:\Dev_Root\python\__name___and___main__>__name___and___main__.py
test: square(42) == 1764
In caller, test for mymath: square(12)= 144

此处,我们看到了,也同时出现了,原本用于mymath.py中去测试square函数的打印结果:

test: square(42) == 1764

而这样的内容,很明显,是作为我模块的调用者,不希望看到的。也不关心的。

 

此时,我们所希望的是:

作为模块mymath.py本身,希望有自己的相关的调试的代码,用于调试自己模块函数,演示如何使用等等代码;

但是又不希望在被别的,本模块的调用者,所执行到,所看到;

此时,就可以用上述的__main__来实现了:

把mymath.py改为:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
【整理】Python中的__name__和__main__含义详解
 
http://www.crifan.com/python_detailed_explain_about___name___and___main__
 
Version:    2012-11-17
Author:     Crifan
Contact:    http://www.crifan.com/contact_me/
"""
 
def square(x):
    return * x
 
if __name__ == "__main__":
    print "test: square(42) ==",square(42);

此时:

1. 作为mymath.py本身,自己运行时,可以运行到此处的代码,调试,验证自己的函数square执行的是否正确:

?
1
2
E:\Dev_Root\python\__name___and___main__>mymath.py
test: square(42) == 1764

2.同时,作为mymath.py的调用者__name___and___main__.py,在import mymath的时候,也不会看到对应的代码执行的结果了:

?
1
2
E:\Dev_Root\python\__name___and___main__>__name___and___main__.py
In caller, test for mymath: square(12)= 144

 

其中的__main__,就是:

作为模块mymath.py本身:

  • 作为脚本自己去运行的话,对应的模块名,就是上面所解释的,始终叫做__main__
    • 关于这点,上述代码已经验证过了。因为mymath.py中的__name__,就是对应的,内置的变量,通过判断,的确是__main__,然后才会去执行到对应的模块的测试代码的。
    • 如果被当做一个模块被别人调用的时候,对应的模块mymath.py的模块名,就是mymath;
      • 关于这点,我们可以来验证一下,把__name___and___main__.py改为:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
【整理】Python中的__name__和__main__含义详解
 
http://www.crifan.com/python_detailed_explain_about___name___and___main__
 
Version:    2012-11-17
Author:     Crifan
Contact:    http://www.crifan.com/contact_me/
"""
 
import mymath;
 
print "In caller, test for mymath: square(12)=",mymath.square(12);
print "As module be imported, mymath module name=",mymath.__name__;
      • 再去运行,就可以看到输出的结果是mymath了:
?
1
2
3
E:\Dev_Root\python\__name___and___main__>__name___and___main__.py
In caller, test for mymath: square(12)= 144
As module be imported, mymath module name= mymath

示例2

 

另外,这里:

A Byte of Python  – A module’s __name__

也有个例子,相对更加简洁,需要的可以参考一下。

摘录其代码如下:

?
1
2
3
4
5
6
7
#!/usr/bin/python
# Filename: using_name.py
 
if __name__ == '__main__':
    print 'This program is being run by itself'
else:
    print 'I am being imported from another module'

 

【总结】

__name__:表示模块,类等的名字;

__main__:模块,xxx.py文件本身:

  • 被直接执行时,对应的模块名就是__main__了
    • 可以在
    • if __name__ == “__main__”:
    • 中添加你自己想要的,用于测试模块,演示模块用法等代码。
  • 作为模块,被别的Python程序导入(import)时,模块名就是本身文件名xxx了。

 


【后记 2012-12-27】

后来又专门针对上述的

A Byte of Python  – A module’s __name__

的代码去测试了一下实际效果。

其中,此处由于我中间包含了中文,所以必须添加对应的coding声明,否则,是会出现错误:

D:\tmp\tmp_dev_root\python\tutorial_summary\__name___and___main__>name_and_main.py
File “D:\tmp\tmp_dev_root\python\tutorial_summary\__name___and___main__\name_and_main.py”, line 6
SyntaxError: Non-ASCII character ‘\xe3′ in file D:\tmp\tmp_dev_root\python\tutorial_summary\__name___and___main__\name_and_main.py on line 7, but no encoding declared; see http://www.python.org/peps/p
ep-0263.html for details

添加了coding声明后,就正常了。

然后,最终的测试的代码是:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Filename: name_and_main.py
 
"""
Function:
【整理】Python中的__name__和__main__含义详解
 
http://www.crifan.com/python_detailed_explain_about___name___and___main__/
 
Author:     Crifan Li
Version:    2012-12-27
Contact:    admin at crifan dot com
"""
 
if __name__ == '__main__':
    print 'This program is being run by itself' #This program is being run by itself
else:
    print 'I am being imported from another module'

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/python
# -*- coding: utf-8 -*-
#Filename: caller_demo.py
 
"""
Function:
【整理】Python中的__name__和__main__含义详解
 
http://www.crifan.com/python_detailed_explain_about___name___and___main__/
 
Author:     Crifan Li
Version:    2012-12-27
Contact:    admin at crifan dot com
"""
 
import name_and_main;
 
print "Demo call name_and_main.py";
# I am being imported from another module
# demo call name_and_main.py

最终,测试出来的效果是:

?
1
2
3
4
5
6
D:\tmp\tmp_dev_root\python\tutorial_summary\__name___and___main__>caller_demo.py
I am being imported from another module
Demo call name_and_main.py
 
D:\tmp\tmp_dev_root\python\tutorial_summary\__name___and___main__>name_and_main.py
This program is being run by itself

总的来说,的确是我们所要的效果的,即:

python文件本身自己运行时,显示的是:This program is being run by itself

Python文件被别人import导入时,显示的是:I am being imported from another module



http://stackoverflow.com/questions/419163/what-does-if-name-main-do


up vote671down voteaccepted

Expanding a bit on Harley's answer...

When the Python interpreter reads a source file, it executes all of the code found in it. Before executing the code, it will define a few special variables. For example, if the python interpreter is running that module (the source file) as the main program, it sets the special __name__ variable to have a value "__main__". If this file is being imported from another module, __name__ will be set to the module's name.

In the case of your script, let's assume that it's executing as the main function, e.g. you said something like

python threading_example.py

on the command line. After setting up the special variables, it will execute the import statement and load those modules. It will then evaluate the def block, creating a function object and creating a variable called myfunction that points to the function object. It will then read the if statement and see that __name__ does equal "__main__", so it will execute the block shown there.

One of the reasons for doing this is that sometimes you write a module (a .py file) where it can be executed directly. Alternatively, it can also be imported and used in another module. By doing the main check, you can have that code only execute when you want to run the module as a program and not have it execute when someone just wants to import your module and call your functions themselves.

See this page for some extra details.

share|improve this answer
 
51 
+1 for "If this file is being imported from another module, __name__ will be set to the module's name." I didn't know that. –  Nawaz Jul 25 '13 at 9:22 
add comment
up vote219down vote

When your script is run by passing it as a command to the Python interpreter,

python myscript.py

all of the code that is at indentation level 0 gets executed. Functions and classes that are defined are, well, defined, but none of their code gets ran. Unlike other languages, there's no main() function that gets run automatically - the main() function is implicitly all the code at the top level.

In this case, the top-level code is an if block. __name__ is a built-in variable which evaluate to the name of the current module. However, if a module is being run directly (as in myscript.py above), then __name__ instead is set to the string "__main__". Thus, you can test whether your script is being run directly or being imported by something else by testing

if __name__ == "__main__":    ...

If that code is being imported into another module, the various function and class definitions will be imported, but the main() code won't get run. As a basic example, consider the following two scripts:

# file one.pydef func():    print("func() in one.py")print("top-level in one.py")if __name__ == "__main__":    print("one.py is being run directly")else:    print("one.py is being imported into another module")# file two.pyimport oneprint("top-level in two.py")one.func()if __name__ == "__main__":    print("two.py is being run directly")else:    print("two.py is being imported into another module")

Now, if you invoke the interpreter as

python one.py

The output will be

top-level in one.pyone.py is being run directly

If you run two.py instead:

python two.py

You get

top-level in one.pyone.py is being imported into another moduletop-level in two.pyfunc() in one.pytwo.py is being run directly

Thus, when module one gets loaded, its __name__ equals "one" instead of __main__.

share|improve this answer
 
4 
And by merely importing a script, all this script's top-level code gets executed first? –  skyork Mar 3 '12 at 0:58
 
@skyork Yes, you are correct. –  ƊŗęДdϝul Ȼʘɗɇ Jul 5 '13 at 8:30
add comment
up vote165down vote

The simplest explanation for the __name__ variable (imho) is the following:

Create the following files.

# a.pyimport b

and

# b.pyprint "Hello World from %s!" % __name__if __name__ == '__main__':    print "Hello World again from %s!" % __name__

Running them will get you this output:

$ python a.pyHello World from b!$ python b.pyHello World from __main__!Hello World again from __main__!

0 0