写一个hello模块
#!/usr/sbin/env python #-*- coding:utf- -*- print "我是hello模块,我被执行了"
在另一个python程序中导入这个模块
#!/usr/sbin/env python #-*- coding:utf-8 -*- import hello
执行这个程序:
我们的本意只是导入这个模块,而不想执行这个模块
C:\Python27\python.exe E:/deploy/m/h.py 我是hello模块,我被执行了 Process finished with exit code 0
为了在导入模块的时候不自动执行模块中的方法我们可以这么做:
#!/usr/sbin/env python #-*- coding:utf-8 -*- if __name__ == "__main__": print "我是hello模块,我被执行了"
加iif __name__ == "__main__":语句之后并不影响改程序在终端中的执行
[root@vm_134 tmp]# python hello.py 我是hello模块,我被执行了 [root@vm_134 tmp]#