CentOS7系统目前默认python环境版本号为2.7.5,在实际的开发、测试环境中我们可能需要2.7.13的版本,但是Centos操作系统自带软件可能依赖python2.7.5版本,故原版本不能删除,我们只能python基础版本与高版本并存,以下为具体步骤:
1、查看Centos版本及python版本:
[root@localhost ~]# cat /etc/centos-release
CentOS Linux release 7.3.1611 (Core)
[root@localhost ~]# python -V
Python 2.7.5
2、从官网下载相应的Python包(以2.7.13为例):
wget https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tgz
3、解压Python包
tar -zxvf Python-2.7.13.tgz
4、配置编译环境
yum install gcc* openssl openssl-devel ncurses-devel.x86_64 bzip2-devel sqlite-devel python-devel zlib
5、配置、编译、安装
[root@localhost ~]# cd Python-2.7.13
[root@localhost Python-2.7.13]# ./configure --prefix=/usr/local #[配置]指定可执行文件、库文件、配置文件、资源文件的安装路径。若没有权限加sudo
[root@localhost Python-2.7.13]# make #编译
[root@localhost Python-2.7.13]# make altinstall #不要使用make install,否则会覆盖系统自带python
6、安装后环境检查
备份旧版本
[root@localhost Python-2.7.13]# mv /usr/bin/python /usr/bin/python2.7.5
访问老版本
[root@localhost bin]# python2.7.5
链接新版本
[root@localhost Python-2.7.13]# ln -s /usr/local/bin/python2.7 /usr/bin/python
查看当前版本
[root@localhost bin]# python -V
Python 2.7.13
修改yum配置
将下列两个文件首行的 #!/usr/bin/python 都改为 #!/usr/bin/python2.7.5
[root@localhost bin]# vim /usr/bin/yum
[root@localhost bin]# vim /usr/libexec/urlgrabber-ext-down
7、安装pip
wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py