CentOS 6.5升級Python和安裝pip、IPython

原文地址:http://www.noanylove.com/2014/10/centos-6-5-sheng-ji-python-he-an-zhuang-ipython/

以前用CentOS 5.8的時候,寫過一篇《升級CentOS的Python》。後來換成了CentOS 6.5,系統自帶的Python版本是2.6.6,就懶得折騰。最近安裝IPython,才發現IPython 2.3需要Python 2.7或者大於等於3.3,如圖一所示。只有升級Python了。

圖一

圖一:安裝IPython需求

已經安裝好gcc等編譯工具。系統自帶Python的版本和安裝位置如圖二所示:

圖二

圖二:系統自帶Python的安裝信息

安裝步驟

1、安裝開發庫文件

編譯Python只要有gcc編譯器就足夠了,但一些擴展模塊需要額外的庫,否則一些Python模塊將不可用(比如Python的zlib模塊需要zlib-devel,ssl模塊需要openssl-devel)。用戶可以根據需要,選擇性的安裝這些擴展模塊。這裏我們安裝zlib、ssl和sqlite3的庫文件。

 yum install zlib-devel openssl-devel sqlite-devel

2、下載Python

目前最新版本爲2.7.8。

wget https://www.python.org/ftp/python/2.7.8/Python-2.7.8.tgz
tar zxvf Python-2.7.8.tgz

3、編譯安裝

爲了方便管理,將Python安裝到/usr/local/python路徑下。

cd Python-2.7.8 
./configure --prefix=/usr/local/python
make
make install

4、替換系統自帶Python

安裝後,Python 2.7.8的可執行文件位於/usr/local/python/bin。先將系統自帶的Python重命名爲python2.6,再創建新的Python到/usr/bin目錄下的符號鏈接。

mv /usr/bin/python /usr/bin/python2.6  #其實不要這句也行,加上了預防萬一
ln -sf /usr/local/python/bin/python2.7 /usr/bin/python

再執行python --version,可以看到已經是2.7.8版了,如圖三所示。

圖三

圖三:成功安裝Python 2.7.8
5、修正yum

現在,系統默認調用Python 2.7.8,執行yum會出現如圖四所示錯誤。

圖四

圖四:yum無法執行
解決方法:將/usr/bin/yum文件開頭的第一行代碼:

#!/usr/bin/python

修改爲:

#!/usr/bin/python2.6

6、安裝setuptools和pip

玩Python不能沒有setuptools和pip。這裏使用pip提供的安裝腳本,自動安裝setuptools和pip。

wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py

因爲之前更改了Python的安裝目錄,Python擴展模塊的可執行文件會被安裝到/usr/local/python/bin目錄下。爲了能夠直接運行這些文件,我們要麼創建到/usr/bin目錄下的符號鏈接,要麼將/usr/local/python/bin目錄添加到PATH環境變量中。爲了避免不必要的衝突,這裏採用創建符號鏈接的方式。

ln -sf /usr/local/python/bin/pip /usr/bin/pip
ln -sf /usr/local/python/bin/easy_install /usr/bin/easy_install

7、安裝IPython

有了pip,安裝IPython簡直So easy。(同樣將IPython的可執行文件鏈接到/usr/bin目錄下)

pip install ipython
ln -s /usr/local/python/bin/ipython /usr/bin/ipython

如圖五所示。

圖五

圖五:安裝IPython
8、安裝readline擴展模塊

沒有readline支持的IPython弱得沒法用。這裏安裝readline:

yum install readline-devel
yum install patch  #相信我,不安裝這個就無法編譯readline
pip install readline

之後,IPython的高亮顯示和代碼補完等功能就可以用了,如圖六所示。當然,現在的IPython只具備了最基本的功能,還有一些模塊沒有安裝,所以無法使用諸如並行計算、Qt Console、Notebook等功能。不過這不是我們這篇文章要討論的內容了。以後有時間會寫一篇安裝使用IPython其他模塊和功能的文章。

圖六

圖六:安裝好的IPython

參考文檔 

  補充 

  第6步以後: 
  #  which pip
/usr/local/python3.5.1/bin/pip
   /usr/local/python3.5.1/bin下的多出了:
         easy_install、easy_install-3.5
      pip、pip3、pip3.5
  #  ln -sf /usr/local/python3.5.1/bin/easy_install-3.5 /usr/bin/easy_install

  第7步的安裝過程顯示:
  # pip install ipython
Collecting ipython
  Downloading ipython-4.2.0-py3-none-any.whl (736kB)
    100% |################################| 737kB 11kB/s
Collecting simplegeneric>0.8 (from ipython)
  Downloading simplegeneric-0.8.1.zip
Collecting setuptools>=18.5 (from ipython)
  Downloading setuptools-21.0.0-py2.py3-none-any.whl (509kB)
    100% |################################| 512kB 22kB/s
Collecting traitlets (from ipython)
  Downloading traitlets-4.2.1-py2.py3-none-any.whl (67kB)
    100% |################################| 71kB 42kB/s
Collecting pexpect (from ipython)
  Downloading pexpect-4.0.1.tar.gz (143kB)
    100% |################################| 153kB 7.3kB/s
Collecting decorator (from ipython)
  Downloading decorator-4.0.9-py2.py3-none-any.whl
Collecting backports.shutil-get-terminal-size (from ipython)
  Downloading backports.shutil_get_terminal_size-1.0.0-py2.py3-none-any.whl
Collecting pickleshare (from ipython)
  Downloading pickleshare-0.7.2-py2.py3-none-any.whl
Collecting ipython-genutils (from traitlets->ipython)
  Downloading ipython_genutils-0.1.0-py2.py3-none-any.whl
Collecting ptyprocess>=0.5 (from pexpect->ipython)
  Downloading ptyprocess-0.5.1-py2.py3-none-any.whl
Building wheels for collected packages: simplegeneric, pexpect
  Running setup.py bdist_wheel for simplegeneric ... done
  Stored in directory: /root/.cache/pip/wheels/7b/31/08/c85e74c84188cbec6a6827beec4d640f2bd78ae003dc1ec09d
  Running setup.py bdist_wheel for pexpect ... done
  Stored in directory: /root/.cache/pip/wheels/f2/65/89/09578bcd0efeabc7e2b0079cd62d3955c1477f2e55aa5031a4
Successfully built simplegeneric pexpect
Installing collected packages: simplegeneric, setuptools, ipython-genutils, decorator, traitlets, ptyprocess, pexpect, backports.shutil-get-terminal-size, pickleshare, ipython
  Found existing installation: setuptools 18.2
    Uninstalling setuptools-18.2:
      Successfully uninstalled setuptools-18.2
Successfully installed backports.shutil-get-terminal-size-1.0.0 decorator-4.0.9 ipython-4.2.0 ipython-genutils-0.1.0 pexpect-4.0.1 pickleshare-0.7.2 ptyprocess-0.5.1 setuptools-21.0.0 simplegeneric-0.8.1 traitlets-4.2.1


    
    
 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章