模块
python允许模块导入。这就允许把已有的模块拿来为我所用了。考虑到目前python社区的发展,以及一个运维团队自身开发出来的python脚本,其数量之巨大无疑会大大加快python的开发容易程度。
现将python的模块导入用法摘抄如下:
1. 一个简单的模块脚本:
hello.py
print "Hello World!"
将模块保存到~/python目录下面。
将模块所在的路径添加到sys.path里面
import sys
sys.path.append('~/python')
import hello
这样就可以在路径下面找到这个python程序
2. 在模块中添加测试代码
#hello3.py
def hello():
print "Hello World!"
#A test
def test():
hello()
#double _ before name and main
if __name__ == '__main__':test()
这样在模块导入的时候,这个值就会被设置为模块的名字,因此在导入的时候是不会被执行的。
>>> import hello3
>>> hello3.test()
Hello World!