需求说明:
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指令。