linux設定python環境變數
阿新 • • 發佈:2019-02-11
新增環境變數主要是在/etc/profile中
例如,在/usr/local/下安裝了python2.7
因為系統預設是2.4,所以
#python
Python 2.4.3 (#1, Sep
3 2009, 15:37:37)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-46)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
進入python2.7的安裝路徑,在/bin下找到python檔案
./python
Python 2.7 .2 (default, Feb 2 2012, 11:45:14)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-51)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
先檢視下目前的環境變數
#echo $PATH
/usr/local/jdk1.5/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
預設是沒有最新的python2.7的
我進入新裝的python2.7目錄檢視
/usr/local/python/bin/python2.7
這個就是python2.7的啟動檔案
# PATH=$PATH:/usr/local/python/bin/
# echo $PATH
/usr/local/jdk1.5/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/python/bin/
將新裝的/usr/local/python/bin新增到環境變數中(但這個只是臨時的做法,退出python後就無效了)
永久有效的做法是:進入/etc/profile,
PATH=$PATH:/usr/local/python/bin
export PATH
這時當我們輸入
# python2.7
Python 2.7.2 (default, Feb 2 2012, 11:45:14)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-51)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
因為系統會自動在環境中找尋python2.7這個檔案並啟動,但會優先找/usr/bin下的啟動檔案,所以
當#python時,還是會進入2.4的預設系統
這時我們可以利用軟連線來做到直接進入python2.7,進入/usr/bin,建立連線
ln -s /usr/local/python/bin/python ./python27
這樣我們#python27 的時候,就會直接進入2.7的環境了