CentOS下python2.6升级2.7

摘要

貌似CentOS 6.X系统默认安装的Python都是2.6版本的,平时使用以及很多的库都是要求用到2.7版本或以上,所以系统升级Python2.7是不可避免的


CentOS下python2.6升级2.7

Linux

date 2018年5月18日15:51:45

查看当前系统版本

  1. Python 2.6.6 (r266:84292, Nov 22 2013, 12:16:22)
  2. [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
  3. Type "help", "copyright", "credits" or "license" for more information.
  4. dlopen("/usr/lib64/python2.6/lib-dynload/readline.so", 2);
  5. import readline # dynamically loaded from /usr/lib64/python2.6/lib-dynload/readline.so
  6. >>>

1.安装devtoolset

  1. $ yum groupinstall "Development tools"

2.安装编译Python需要的包

  1. yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel

3.下载并解压Python 2.7.9的源代码

  1. $ cd /tmp/
  2. $ wget --no-check-certificate https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tar.xz
  3. $ tar xf Python-2.7.9.tar.xz
  4. $ cd Python-2.7.9

4.编译与安装Python 2.7.9

  1. $ ./configure --prefix=/usr/local
  2. $ make && make altinstall

5.配置软连接,让系统pyhon默认指向python2.7.9

  1. 备份
  2. mv /usr/bin/python /usr/bin/python2.6_temp
  3. 制作软连接
  4. ln -s /usr/local/bin/python2.7 /usr/bin/python
  5. $ python -V # 查看Python 2.7.9

6.yum与python2.7不兼容,修改yum文件

  1. vim /usr/bin/yum
  2. "!/usr/bin/python"改为"!/usr/bin/python2.6"

image_1cdp3eiregqc1m713svqvt16819.png-54.7kB

7.配置环境变量

  1. $ vim /etc/profile
  2. # 添加如下内容:
  3. PY_HOME=/usr/local/bin/python2.7
  4. export PATH=$PY_HOME/bin:$PATH

8.启动环境变量配置

  1. source /etc/profile or . /etc/profile
  2. echo $PATH #查看环境变量中是否包含python路径
  3. [root@abcdocker ~]# echo $PATH
  4. /usr/local/bin/python2.7/bin:/usr/local/node/bin:/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin

9.完美升级完成
QQ图片20180518155047.gif-54.5kB