天天看點

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了。

CentOS 6.5更新Python和安裝pip、IPython

圖一:安裝IPython需求

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

CentOS 6.5更新Python和安裝pip、IPython

圖二:系統自帶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版了,如圖三所示。

CentOS 6.5更新Python和安裝pip、IPython

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

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

CentOS 6.5更新Python和安裝pip、IPython

圖四:yum無法執行 解決方法:将/usr/bin/yum檔案開頭的第一行代碼:

修改為:

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
           

如圖五所示。

CentOS 6.5更新Python和安裝pip、IPython

圖五:安裝IPython 8、安裝readline擴充子產品

沒有readline支援的IPython弱得沒法用。這裡安裝readline:

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

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

CentOS 6.5更新Python和安裝pip、IPython

圖六:安裝好的IPython

參考文檔 

  • http://blog.csdn.net/handsomekang/article/details/11693899
  • https://pip.pypa.io/en/latest/installing.html
  • http://ipython.org/ipython-doc/2/install/install.html

  補充 

  第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

CentOS 6.5更新Python和安裝pip、IPython