-
python 文件的后缀为.py,比如 name.py
-
python 文件可以直接执行,也可以被导入、调用,比如import name;
script 1:
|
1
2
3
4
5
6
|
#!/bin/python# Filename:name.pyif __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.pyimport name; |
|
1
2
3
4
5
|
执行 test.py返回C:\Python27\python.exe C:/Users/Administrator/PycharmProjects/11/test.pyI am being imported from another moduleProcess finished with exit code 0 |
结论:
-
__name__=='__main__' 代表直接执行
-
如果被当作模块导入到其他python文件中,执行else 后面的命令
-
一般我们在调试的时候会用到
浙公网安备 33010602011771号