import 導入模塊學習

import 導入模塊學習

   python 大家都知道,模塊是這門語言的強大之處,那麼經常導入模塊去使用也是做爲程序員或者是運維工程師必須要做的,那我們也深入學習下吧

   導入系統已安裝的模塊

[root@localhost bin]# python
Python 2.6.6 (r266:84292, Dec  7 2011, 20:48:22)
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> import re
>>> import os
>>>

   各位,看清楚了,這個沒有問題吧,但是,這些模塊是存入在哪裏,或者是說路徑在哪裏呢?


[root@localhost bin]# python
Python 2.6.6 (r266:84292, Dec  7 2011, 20:48:22)
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib64/python26.zip', '/usr/lib64/python2.6', '/usr/lib64/python2.6/plat-linux2', '/usr/lib64/python2.6/lib-tk', '/usr/lib64/python2.6/lib-old', '/usr/lib64/python2.6/lib-dynload', '/usr/lib64/python2.6/site-packages', '/usr/lib64/python2.6/site-packages/gtk-2.0', '/usr/lib/python2.6/site-packages']
>>>

   以後大家使用的時候也可以這樣查詢你安裝了啥模塊,接下來就是,安裝了模塊是可以直接使用 的,那如果是我自己寫的模塊,我也想直接使用,那怎麼樣操作

[root@localhost python]# cat module.py
test='This is my module'
print test
[root@localhost python]# python
Python 2.6.6 (r266:84292, Dec  7 2011, 20:48:22)
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import module
This is my module
>>>

  這樣就可以使用,但接下來要注意的一件事情,如果我不在模塊的目錄下面執行那會不會成功?

[root@localhost python]# pwd
/opt/python
[root@localhost python]# cd ..
[root@localhost opt]# python
Python 2.6.6 (r266:84292, Dec  7 2011, 20:48:22)
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import module
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named module
>>>

   發現問題了吧,提示說沒有這個模塊,那怎麼樣才能正常使用自己創建模塊呢?因爲總不能到模塊目錄去寫程序吧?

>>> dir(sys.path)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

  這個查找sys.path的方法,當我們不知道這個模塊怎麼樣使用的話,這是最基礎的查找和使用

看這裏是不是有一個append?,這個就是可以添加模塊的路徑的,那麼我們來試一

[root@localhost opt]# pwd
/opt
[root@localhost opt]# python
Python 2.6.6 (r266:84292, Dec  7 2011, 20:48:22)
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append('/opt/python')
>>> import module
This is my module
>>>


   這樣添加了路徑就可以添加了,那接下來現試下,如果我退出後,再重新導入模塊會是怎麼樣的?

[root@localhost opt]# pwd
/opt
[root@localhost opt]# python
Python 2.6.6 (r266:84292, Dec  7 2011, 20:48:22)
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append('/opt/python')
>>> import module
This is my module
>>>
[root@localhost opt]# python
Python 2.6.6 (r266:84292, Dec  7 2011, 20:48:22)
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import module
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named module
>>>

  說明sys.path.append也只是一次性能把自己的模塊路徑給添加,退出後則沒有了,這樣用起來也不是很方便。用更好的辦法

[root@localhost opt]# export PYTHONPATH=/opt/python/
[root@localhost opt]# python
Python 2.6.6 (r266:84292, Dec  7 2011, 20:48:22)
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import module
This is my module
>>>

  使用export添加,這樣就永久啦,所以自己以後的開發,模塊都放在一起,方便使用

在下也是菜鳥,希望各位有經驗的大牛多多指點



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章