linux系統中python版本升級

一,查看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,保存退出便可

相關文章
相關標籤/搜索