python 文件的后缀为.py,比如 name.py
python 文件可以直接执行,也可以被导入、调用,比如import name;
script 1:
1
2
3
4
5
6
|
#!/bin/python # Filename:name.py 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 # filename:test.py 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 后面的命令
一般我们在调试的时候会用到
原文:http://www.cnblogs.com/tmdba/p/6381996.html