Centos升级python2.7

CentOS 6.X 自带的Python版本是 2.6 , 目前python主流的编译环境是2.7,故整理一下python2.6到2.7的升级过程。

环境准备

依赖工具包

1
sudo yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel

源码安装2.7

1
2
3
wget http://www.python.org/ftp/python/2.7.8/Python-2.7.8.tar.xz
xz -d Python-2.7.8.tar.xz
tar -xvf Python-2.7.8.tar

安装

1
2
3
4
5
6
cd Python-2.7.8
./configure --prefix=/usr/local
make
make altinstall
[root@VM_centos ~]# python2.7 -V
Python 2.7.8

更新python

1
2
mv /usr/bin/python /usr/bin/python.bak
ln -s /usr/local/bin/python2.7 /usr/bin/python

此时

1
2
3
4
5
[clouddev@TY-0064 ~]$ python -V
Python 2.7.8
[clouddev@TY-0064 ~]$ which python
/usr/bin/python
[clouddev@TY-0064 ~]$

安装pip

1
curl https://bootstrap.pypa.io/get-pip.py | python2.7 -

启动pip报错

1
pkg_resources.DistributionNotFound: The 'pip==7.1.0' distribution was not found and is required by the application

修改pip

1
2
mv /usr/bin/pip /usr/bin/pip0
cp /usr/local/bin/pip2.7 /usr/bin/pip

修复yum

yum依赖的python2.6,此时yum无法使用

1
2
[clouddev@TY-0064 ~]$ which yum
/usr/bin/yum

将第一行#!/usr/bin/python改为#!/usr/bin/python2.6