文章目录
  1. 1. 在CentOS 6.X 上面安装 Python 2.7.X
  2. 2. 升级步骤
    1. 2.1. 更新系统和开发工具集
    2. 2.2. 源码安装Python 2.7.x
      1. 2.2.1. 安装详情:
      2. 2.2.2. 设置 PATH
      3. 2.2.3. 安装 setuptools
    3. 2.3. 安装 PIP
    4. 2.4. 修复 yum 工具
  3. 3. 总结

在CentOS 6.X 上面安装 Python 2.7.X

CentOS 6.X 自带的python版本是 2.6 , 由于工作需要,很多时候需要2.7版本。所以需要进行版本升级。由于一些系统工具和服务是对 Python 有依赖的,所以升级 Python 版本需要注意。

升级步骤

如何欢乐的,没有痛苦的升级python版本 ,往下看 …

更新系统和开发工具集

更新指令

1
2
yum -y update
yum groupinstall -y 'development tools'

另外还需要安装 python 工具需要的额外软件包 SSL, bz2, zlib

1
yum install -y zlib-devel bzip2-devel openssl-devel xz-libs wget

源码安装Python 2.7.x

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
7
8
9
10
# 进入目录:
cd Python-2.7.8
# 运行配置 configure:
./configure --prefix=/usr/local
# 编译安装:
make
make altinstall
# 检查 Python 版本:
[root@dbmasterxxx ~]# python2.7 -V
Python 2.7.8

设置 PATH

为了我们能够方便的使用Python,我们需要设置系统变量或者建立 软连接将新版本的 Python
加入到 path 对应的目录 :

1
2
3
4
5
6
7
8
export PATH="/usr/local/bin:$PATH"
or
ln -s /usr/local/bin/python2.7 /usr/bin/python
# 检查
[root@dbmasterxxx ~]# python -V
Python 2.7.8
[root@dbmasterxxx ~]# which python
/usr/bin/python

安装 setuptools

#获取软件包
wget --no-check-certificate https://pypi.python.org/packages/source/s/setuptools/setuptools-1.4.2.tar.gz
# 解压:
tar -xvf setuptools-1.4.2.tar.gz
cd setuptools-1.4.2
# 使用 Python 2.7.8 安装 setuptools
python2.7 setup.py install

安装 PIP

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

修复 yum 工具

此时yum应该是失效的,因为此时默认python版本已经是2.7了。而yum需要的是2.6 所以:

[root@dbmasterxxx ~]# which yum 
/usr/bin/yum
#修改 yum中的python 
将第一行  #!/usr/bin/python  改为 #!/usr/bin/python2.6
此时yum就ok啦

总结

Python 版本升级过很多遍,每次都有问题,此方法来自互联网,经过使用,没有问题,特此总结一下
文章目录
  1. 1. 在CentOS 6.X 上面安装 Python 2.7.X
  2. 2. 升级步骤
    1. 2.1. 更新系统和开发工具集
    2. 2.2. 源码安装Python 2.7.x
      1. 2.2.1. 安装详情:
      2. 2.2.2. 设置 PATH
      3. 2.2.3. 安装 setuptools
    3. 2.3. 安装 PIP
    4. 2.4. 修复 yum 工具
  3. 3. 总结