一,查看python版本號
python -V
二,下載需要升級到python版本包下載地址https://www.python.org/ftp/python/
根據需要選擇需要的python版本
eg:wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz
三,解壓編譯安裝python
tar -zxvf Python-3.5.2.tgz
安裝配置
cd Python-3.5.2
./configure
執行 ./configure 時,如果報錯:
configure: error: no acceptable C compiler found in $PATH
說明沒有安裝合適的編譯器。這時,需要安裝/升級 gcc 及其它依賴包。
# yum install make gcc gcc-c++
1
完成之后,重新執行:
# ./configure
編譯安裝
make && make install
安裝成功以后,就可以查看 Python 的版本了:
# python -V
Python 2.7.5
# python3 -V
Python 3.5.2
一個是舊版本 2.x,另外一個是新版本 3.x。
注意:在 /usr/local/bin/ 下有一個 python3 的鏈接,指向 bin 目錄下的 python 3.5
四,設置3.X python 為默認版本
查看 Python 的路徑,在 /usr/bin 下面。可以看到 python 鏈接的是 python 2.7,所以,執行 python 就相當於執行 python 2.7。
# ls -al /usr/bin | grep python
-rwxr-xr-x. 1 root root 11216 12月 1 2015 abrt-action-analyze-python
lrwxrwxrwx. 1 root root 7 8月 30 12:11 python -> python2
lrwxrwxrwx. 1 root root 9 8月 30 12:11 python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 11月 20 2015 python2.7
將原來 python 的軟鏈接重命名:
# mv /usr/bin/python /usr/bin/python.bak
將 python 鏈接至 python3:
# ln -s /usr/local/bin/python3 /usr/bin/python
這時,再查看 Python 的版本:
# python -V
Python 3.5.2
輸出的是 3.x,說明已經使用的是 python3了
---------------------
配置 yum
升級 Python 之后,由於將默認的 python 指向了 python3,yum 不能正常使用,需要編輯 yum 的配置文件:
# vi /usr/bin/yum
1
同時修改:
# vi /usr/libexec/urlgrabber-ext-down
1
將 #!/usr/bin/python 改為 #!/usr/bin/python2.7,保存退出即可