脚本和模块

来自Shiyin's note
跳到导航 跳到搜索
  • 模块不能直接运行
  • 脚本可以
  • 参考http://blog.csdn.net/kenkywu/article/details/6743462

创建一个test.py

def test2():
   print "test2 is running"
if __name__ == "__main__":#自运行时调用该程序块
   print "test main is working"
if __name__ == "test":#import时调用该程序块
   print "test is invoked"

>>>python test.py test main is working >>>import test test is invoked >>> test.test2()

print "test2 is running"