-
python 文件的后缀为.py,比如 name.py
-
python 文件可以直接执行,也可以被导入、调用,比如import name;
script 1:
1
2
3
4
5
6
|
#!/bin/python
if __name__== '__main__' :
print 'This program is being run by itself'
else :
print 'I am being imported from another module'
|
script 2:
1
2
3
|
#!/bin/python
import name;
|
1
2
3
4
5
|
执行 test .py
返回
C:\Python27\python.exe C: /Users/Administrator/PycharmProjects/11/test .py
I am being imported from another module
Process finished with exit code 0
|
结论:
-
__name__=='__main__' 代表直接执行
-
如果被当作模块导入到其他python文件中,执行else 后面的命令
-
一般我们在调试的时候会用到
本文转自 roidba 51CTO博客,原文链接:http://blog.51cto.com/roidba/1894285,如需转载请自行联系原作者