1. 程式人生 > >linux python 修改環境變量 添加自定義模塊路徑

linux python 修改環境變量 添加自定義模塊路徑

2.0 -i 親測 包導入 發現 b- protoc oca error:

舉一個很簡單的例子,如果你發現一個包或者模塊,明明是有的,但是會發生這樣的錯誤:

>>> from algorithm import *
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named algorithm

那麽就應該是環境變量出問題了

okay,來看怎麽搞

>>>>> import sys
>>> sys.path
[‘‘, ‘/usr/lib/python2.7‘, ‘/usr/lib/python2.7/plat-linux2‘, ‘/usr/lib/python2.7/lib-tk‘, ‘/usr/lib/python2.7/lib-old‘, ‘/usr/lib/python2.7/lib-dynload‘, ‘/usr/local/lib/python2.7/dist-packages‘, ‘/usr/lib/python2.7/dist-packages‘, ‘/usr/lib/python2.7/dist-packages/PIL‘, ‘/usr/lib/python2.7/dist-packages/gst-0.10‘, ‘/usr/lib/python2.7/dist-packages/gtk-2.0‘, ‘/usr/lib/pymodules/python2.7‘, ‘/usr/lib/python2.7/dist-packages/ubuntu-sso-client‘, ‘/usr/lib/python2.7/dist-packages/ubuntuone-client‘, ‘/usr/lib/python2.7/dist-packages/ubuntuone-control-panel‘, ‘/usr/lib/python2.7/dist-packages/ubuntuone-couch‘, ‘/usr/lib/python2.7/dist-packages/ubuntuone-installer‘, ‘/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol‘]

首先,這些顯示出了python自己的環境變量

然後

>>> sys.path.append(‘/media/File/Code_DownloadCodeAndSelfCode/PyWorkPlace/xView‘)
>>> sys.path
[‘‘, ‘/usr/lib/python2.7‘, ‘/usr/lib/python2.7/plat-linux2‘, ‘/usr/lib/python2.7/lib-tk‘, ‘/usr/lib/python2.7/lib-old‘, ‘/usr/lib/python2.7/lib-dynload‘, ‘/usr/local/lib/python2.7/dist-packages‘, ‘/usr/lib/python2.7/dist-packages‘, ‘/usr/lib/python2.7/dist-packages/PIL‘, ‘/usr/lib/python2.7/dist-packages/gst-0.10‘, ‘/usr/lib/python2.7/dist-packages/gtk-2.0‘, ‘/usr/lib/pymodules/python2.7‘, ‘/usr/lib/python2.7/dist-packages/ubuntu-sso-client‘, ‘/usr/lib/python2.7/dist-packages/ubuntuone-client‘, ‘/usr/lib/python2.7/dist-packages/ubuntuone-control-panel‘, ‘/usr/lib/python2.7/dist-packages/ubuntuone-couch‘, ‘/usr/lib/python2.7/dist-packages/ubuntuone-installer‘, ‘/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol‘, ‘/media/File/Code_DownloadCodeAndSelfCode/PyWorkPlace/xView/emotionAnalyse‘, ‘/media/File/Code_DownloadCodeAndSelfCode/PyWorkPlace/xView‘]

>>> import algorithm

這樣,就把自己的algorithm這個包導入了

ok了!

可是這樣有個問題,下次還得設置才行。

那麽就改一下系統變量吧:

$export PYTHONPATH=$PYTHONPATH:/home/YOURSELFPATH

親測有效~

linux python 修改環境變量 添加自定義模塊路徑