import 導入模塊學習

import 導入模塊學習python

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

   導入系統已安裝的模塊程序員

[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
>>>

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


[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
>>>

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

[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
>>>

   發現問題了吧,提示說沒有這個模塊,那怎麼樣才能正常使用本身建立模塊呢?由於總不能到模塊目錄去寫程序吧?ide

>>> 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
>>>


   這樣添加了路徑就能夠添加了,那接下來現試下,若是我退出後,再從新導入模塊會是怎麼樣的?orm

[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添加,這樣就永久啦,因此本身之後的開發,模塊都放在一塊兒,方便使用

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

相關文章
相關標籤/搜索