Centos7下部署兩套python版本並存環境的操作記錄

散盡浮華發表於2017-09-27

 

需求說明:
centos7.2系統的開發機器上已經自帶了python2.7版本,但是開發的專案中用的是python3.5版本,為了保證Centos系統的正常執行,以及節省機器資源(不想因此再申請另外一臺開發機器部署python3.5),所以需要安裝python3.5與python2.7共存的開發環境。具體操作記錄如下:

1)安裝相關包
[root@qd-vpc-rec-dev01 ~]# yum -y install epel-release
[root@qd-vpc-rec-dev01 ~]# yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel

2) 編譯安裝python3.5
[root@qd-vpc-rec-dev01 ~]# yum -y install xz
[root@qd-vpc-rec-dev01 ~]# wget https://www.python.org/ftp/python/3.5.3/Python-3.5.3.tar.xz
[root@qd-vpc-rec-dev01 ~]# tar xf Python-3.5.3.tar.xz -C /usr/local/src/
[root@qd-vpc-rec-dev01 ~]# cd /usr/local/src/Python-3.5.3
[root@qd-vpc-rec-dev01 Python-3.5.3]# ./configure --prefix=/usr/local/python3
[root@qd-vpc-rec-dev01 Python-3.5.3]# make && make install

從 Python 3.4 開始就已經自帶了pip和easy_install(setuptools 包帶的命令) 包管理命令,你可以在 /usr/local/python3/bin/ 目錄下看到,檢視一下已經安裝的擴充套件包:
[root@qd-vpc-rec-dev01 Python-3.5.3]# ll /usr/local/python3/bin/
total 24080
lrwxrwxrwx 1 root root        8 Sep 27 19:40 2to3 -> 2to3-3.5
-rwxr-xr-x 1 root root      109 Sep 27 19:40 2to3-3.5
-rwxr-xr-x 1 root root      250 Sep 27 19:40 easy_install-3.5
lrwxrwxrwx 1 root root        7 Sep 27 19:40 idle3 -> idle3.5
-rwxr-xr-x 1 root root      107 Sep 27 19:40 idle3.5
-rwxr-xr-x 1 root root      222 Sep 27 19:40 pip3
-rwxr-xr-x 1 root root      222 Sep 27 19:40 pip3.5
lrwxrwxrwx 1 root root        8 Sep 27 19:40 pydoc3 -> pydoc3.5
-rwxr-xr-x 1 root root       92 Sep 27 19:40 pydoc3.5
lrwxrwxrwx 1 root root        9 Sep 27 19:40 python3 -> python3.5
-rwxr-xr-x 2 root root 12309757 Sep 27 19:39 python3.5
lrwxrwxrwx 1 root root       17 Sep 27 19:40 python3.5-config -> python3.5m-config
-rwxr-xr-x 2 root root 12309757 Sep 27 19:39 python3.5m
-rwxr-xr-x 1 root root     3088 Sep 27 19:40 python3.5m-config
lrwxrwxrwx 1 root root       16 Sep 27 19:40 python3-config -> python3.5-config
lrwxrwxrwx 1 root root       10 Sep 27 19:40 pyvenv -> pyvenv-3.5
-rwxr-xr-x 1 root root      244 Sep 27 19:40 pyvenv-3.5
[root@qd-vpc-rec-dev01 Python-3.5.3]# /usr/local/python3/bin/pip3 list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
pip (9.0.1)
setuptools (28.8.0)

-----------------------------------------------------------------------------------------------------------------------------
如果要更新pip,操作如下:
[root@qd-vpc-rec-dev01 Python-3.5.3]# /usr/local/python3/bin/pip3 install --upgrade pip
[root@qd-vpc-rec-dev01 Python-3.5.3]# /usr/local/python3/bin/pip3 list
-----------------------------------------------------------------------------------------------------------------------------

3)建立軟連線
[root@qd-vpc-rec-dev01 Python-3.5.3]# ln -s /usr/local/python3/bin/python3 /usr/bin/python3

4) 執行python -V檢視Python是否安裝成功。
[root@qd-vpc-rec-dev01 Python-3.5.3]# python3 -V
Python 3.5.3

自帶的版本為:
[root@qd-vpc-rec-dev01 Python-3.5.3]# python -V          
Python 2.7.5

[root@qd-vpc-rec-dev01 Python-3.5.3]# which python
/usr/bin/python
[root@qd-vpc-rec-dev01 Python-3.5.3]# which python3
/usr/bin/python3
[root@qd-vpc-rec-dev01 Python-3.5.3]# python
Python 2.7.5 (default, Nov  6 2016, 00:28:07) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
[root@qd-vpc-rec-dev01 Python-3.5.3]# python3
Python 3.5.3 (default, Sep 27 2017, 19:38:56) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

也就是說,python指令表示的是2.7.5版本,python3指令表示的3.5.3版本。
開發專案裡具體使用那個版本的python,就使用對應的全路徑的python指令。

相關文章