Python - centos7.3配置python二、3環境與配置各自pip

環境:CentOS-7-x86_64-Everything-1611html

No.1 查看CentOS對Python的默認依賴

[root@cs ~]# ls /usr/bin/python*
/usr/bin/python  /usr/bin/python2  /usr/bin/python2.7

由於CentOS的yum對Python的依賴,咱們在安裝Python的時候,要當心避開,以避免影響yum的使用。python

No.2 準備編譯環境以及配置相關依賴

[root@bogon nee]# yum groupinstall 'Development Tools' && yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel && yum -y epel-release

須要注意的是,這一步必需要作,爲了不後面安裝解釋器時碰見錯誤。linux

No.3 下載Python解釋器及各自的pip包,並解壓

3.1 下載tar包

[root@bogon nee]# cd /usr/local/src/
[root@bogon src]# wget https://www.python.org/ftp/python/3.5.4/Python-3.5.4.tar.xz && wget https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tar.xz && wget --no-check-certificate https://github.com/pypa/pip/archive/9.0.3.tar.gz && wget https://bootstrap.pypa.io/get-pip.py

3.2 解壓tar包

[root@bogon src]# tar -xvf Python-3.5.4.tar.xz && tar -xvf Python-2.7.14.tar.xz && tar -zxvf 9.0.3.tar.gz

No.4 Python2的安裝並創建軟連

4.1 安裝

[root@bogon src]# cd Python-2.7.14/
[root@bogon Python-2.7.14]# ./configure --prefix=/usr/local/python/python27 --enable-optimizations
[root@bogon Python-2.7.14]# make && make install

咱們在 ./configure 這一步,將Python2安裝在/usr/local/python/python27這個目錄下,下面Python3也在/usr/local/python目錄下,這樣便於管理,和安裝其餘版本的解釋器。git

4.2 創建軟連

咱們能夠看到bin下有python和python2和python2.7這三個能夠用的解釋器github

[root@bogon Python-2.7.14]# ls /usr/local/python/python27/bin/ 
2to3 idle pydoc python python2 python2.7 python2.7-config python2-config python-config smtpd.py

任選一個解釋器建軟連都行  sql

[root@bogon Python-2.7.14]# ln -s /usr/local/python/python27/bin/python2 /usr/bin/python27

注意,軟連到/usr/bin的是python27,由於系統默認有python2,因此,咱們不能跟人家同樣...下面其餘的軟連同理,避開系統默認的bootstrap

No.5 Python3的安裝並創建軟連

這裏創建軟連和添加PATH同樣,用哪一種方法都行, 軟連的意思是將python的解釋器添加到 /usr/bin 下,而 /usr/bin 這個路徑在PATH中已經存在的centos

5.1 安裝

[root@bogon Python-2.7.14]# cd ../Python-3.5.4/
[root@bogon Python-3.5.4]# ./configure  --prefix=/usr/local/python/python35 --enable-optimizations
[root@bogon Python-3.5.4]# make && make install

5.2 建軟連

注意:/usr/local/python/python35/bin/ 下的解釋器叫什麼,最好ls一下看看。網絡

[root@bogon Python-3.5.4]# ln -s /usr/local/python/python35/bin/python3 /usr/bin/python35

No.6 配置Python3的pip並創建軟連

6.1 安裝Python3的pip包

[root@bogon Python-3.5.4]# ls /usr/local/src/ && cd ../pip-9.0.3/
9.0.3.tar.gz  pip-9.0.3      Python-2.7.14.tar.xz  Python-3.5.4.tar.xz
get-pip.py    Python-2.7.14  Python-3.5.4
[root@bogon pip-9.0.3]# python35 setup.py install

能夠看到Python3的pip包是pip-9.0.3,因此咱們要切換到此目錄內,執行安裝,這一步是基於前面步驟沒有問題的基礎上執行的。好比你python35的解釋器沒有配好,那麼根本沒法執行pip的安裝。python2.7

6.2 創建pip3的軟連

# [root@bogon pip-9.0.3]# ln -s /usr/local/python/python35/bin/pip /usr/bin/pip  # 這個軟連可不創建,這樣只是沒有默認的pip
[root@bogon pip-9.0.3]# ln -s /usr/local/python/python35/bin/pip /usr/bin/pip35

咱們在用pip的時候,直接pip35就行,下面的Python2的pip同理。

No.7 配置Python2的pip並創建軟連

7.1 安裝Python2的pip包

[root@bogon pip-9.0.3]# cd .. && ls
9.0.3.tar.gz  pip-9.0.3      Python-2.7.14.tar.xz  Python-3.5.4.tar.xz
get-pip.py    Python-2.7.14  Python-3.5.4
[root@bogon src]# python27 get-pip.py

7.2 創建Python2的軟連

[root@bogon src]# ln -s /usr/local/python/python27/bin/pip2 /usr/bin/pip27

No.8 執行測試

[root@cs src]# python35 -V
Python 3.5.4
[root@cs src]# pip35 -V
pip 9.0.3 from /usr/local/python/python35/lib/python3.5/site-packages/pip-9.0.3-py3.5.egg (python 3.5)
[root@cs src]# python27 -V
Python 2.7.14
[root@cs src]# pip27 -V
pip 10.0.1 from /usr/local/python/python27/lib/python2.7/site-packages/pip (python 2.7)

No.9 選擇升級(此步驟可略過)

目前最新的pip包爲10.0.1,若是不升級,在每次使用pip的時候,會提示你升級,固然咱們能夠忽略,若是要升級的話:

pip35 install --upgrade pip    # Python3的pip升級方式
pip27 install --upgrade pip    # Python2的pip升級方式

至此,咱們在centos下成功安裝雙版本的python解釋器並設置各自的pip。只是沒有默認的解釋器和pip,但咱們爲了省事沒有修改系統依賴的yum頭,並且,修改yum頭的話,新手容易失手。

1.  保存默認版本,並創建yum依賴的軟連
    mv /usr/bin/python2.7 /usr/bin/python2.7.5
    ln -s /usr/bin/python2.7.5 /usr/local/bin/python2.7.5
    ll /usr/bin/yum*
    vi /usr/bin/yum*
    vi /usr/libexec/urlgrabber-ext-down
        #!/usr/bin/python —> #!/usr/bin/python2.7.5

3. 準備編譯環境與相關依賴
    yum groupinstall 'Development Tools' && yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel && yum -y epel-release
4. 下載解釋器與pip的tar包,執行解壓
    cd /usr/local/src

    wget https://www.python.org/ftp/python/3.5.4/Python-3.5.4.tar.xz && wget https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tar.xz && wget --no-check-certificate https://github.com/pypa/pip/archive/9.0.3.tar.gz && wget https://bootstrap.pypa.io/get-pip.py

    tar -xvJf Python-3.5.4.tar.xz && tar -xvJf Python-2.7.14.tar.xz && tar -zxvf 9.0.3.tar.gz

# xz壓縮方式,tar打包方式

5. 執行安裝,並創建各自的軟連
    cd Python-3.5.4
    ./configure  --prefix=/usr/local/python/python35 --enable-optimizations

    make && make install
    mkae clean && make distcleancd 

    ln -s /usr/local/python/python35/bin/python3 /usr/bin/python3
    ln -s /usr/local/python/python35/bin/python3 /usr/bin/python
    
    cd /usr/local/src/Python-2.7.14
    ./configure  --prefix=/usr/local/python/python27 --enable-optimizations

    make && make install
    mkae clean && make distcleancd
    ln -s /usr/local/python/python27/bin/python2 /usr/bin/python2

7. 配置python3 的pip

    wget --no-check-certificate https://github.com/pypa/pip/archive/9.0.3.tar.gz


    tar -zxvf 9.0.3.tar.gz
    cd pip-9.0.3
    python3 setup.py install
    ln -s /usr/local/python/python35/bin/pip3 /usr/bin/pip3
    ln -s /usr/local/python/python35/bin/pip /usr/bin/pip
    pip3 install --upgrade pip


python2 配置pip
wget https://bootstrap.pypa.io/get-pip.py
python2 get-pip.py
ln -s /usr/local/python/python27/bin/pip2 /usr/bin/pip2

pip2 install --upgrade pip
測試筆記

that's all

see also:

  https://www.linuxidc.com/Linux/2017-12/149807.htm

  解決yum頭部對Python依賴的問題,強迫症能夠看看

  centos7安裝python3 以及tab補全功能

  [Python3網絡爬蟲開發實戰] 1.1-Python3的安裝

相關文章
相關標籤/搜索