模塊
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!