CentOS 6 系统默认 Python 版本是:2.6.6 平时在使用中遇到很多的库要求是 2.7.x 版本的库,比如使用 ConfigParser 库,在 2.6 版本库就不支持没有 value 值的配置项,需要升级到 2.7 以上的库才行,这次就尝试升级一下 Python 到 2.7.x 版本,记录于此。

Centos 7升级原python 2.7.5至Python 3.7,请看此文:https://blog.51cto.com/10316297/2134736?from=timeline

一、升级 Python 2.7.14 版本

1. 准备安装包,系统是最小化安装

# 下载安装依赖的相关包
[root@vip ~]# yum install vim gcc gcc-c++ make wget -y [root@vip ~]# yum install openssl-devel zlib-devel readline-devel sqlite-devel -y


# 下载[root@vip ~]# cd /usr/local/src
[root@vip
~]# wget https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tgz
 # 解压
[root@vip
~]# tar -zxvf Python-2.7.14.tgz [root@vip ~]# ls
Python-2.7.14  Python-2.7.14.tgz


2. 编译配置安装

[root@vip ~]# cd Python-2.7.14
[root@vip Python-2.7.14]# ./configure --enable-shared --enable-loadable-sqlite-extensions --prefix=/usr/local/python27 --with-zlib --with-ssl
# 其中--enable-loadable-sqlite-extensions是sqlite的扩展,如果需要使用的话则带上这个选项。
[root@vip Python-2.7.14]# vim ./Modules/Setup    # 找到下边这一行内容,去掉注释
#zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz
[root@vip Python-2.7.14]# make && make install

在编译的时候,Python出现了下面的提示信息:

code
1
2
3
4
5
6
Python build finished, but the necessary bits to build these modules were not found:
    _bsddb             _sqlite3           _ssl
    _tkinter           bsddb185           bz2
    dbm                gdbm               readline
    sunaudiodev
To  find the necessary bits,  look in setup.py  in detect_modules()  for the module's name.

当然,每个机器上因为不同的配置,编译报错信息也会不同,比如网上有人的报错就是下面这样的:

code
1
2
3
4
5
6
7
Python build finished, but the necessary bits to build these modules were not found:
    _bsddb             _curses            _curses_panel  
    _sqlite3           _ssl               _tkinter       
    bsddb185           bz2                dbm            
    dl                 gdbm               imageop        
    readline           sunaudiodev        zlib           
To  find the necessary bits,  look in setup.py  in detect_modules()  for the module's name.

无论报错信息如何,意思很明确,我们编译的时候,系统没有办法找到对应的模块信息,为了解决这些报错,我们就需要提前安装依赖包,这些依赖包对应列表如下(不一定完全):

模块依赖说明
_bsddbbsddbInterface to Berkeley DB library。Berkeley数据库的接口
_cursesncursesTerminal handling for character-cell displays。
_curses_panelncursesA panel stack extension for curses。
_sqlite3sqliteDB-API 2.0 interface for SQLite databases。SqlLite,CentOS可以安装sqlite-devel
_sslopenssl-devel.i686TLS/SSL wrapper for socket objects。
_tkinterN/Aa thin object-oriented layer on top of Tcl/Tk。如果不使用桌面程序可以忽略TKinter
bsddb185old bsddb module老的bsddb模块,可忽略。
bz2bzip2-devel.i686Compression compatible with bzip2。bzip2-devel
dbmbsddbSimple “database” interface。
dlN/ACall C functions in shared objects.Python2.6开始,已经弃用。
gdbmgdbm-devel.i686GNU’s reinterpretation of dbm
imageopN/AManipulate raw image data。已经弃用。
readlinereadline-develGNU readline interface
sunaudiodevN/AAccess to Sun audio hardware。这个是针对Sun平台的,CentOS下可以忽略
zlibZlibCompression compatible with gzip

在CentOS下,可以安装这些依赖包:readline-devel,sqlite-devel,bzip2-devel.i686,openssl-devel.i686,gdbm-devel.i686,libdbi-devel.i686,ncurses-libs,zlib-devel.i686。完成这些安装之后,可以再次编译,上表中指定为弃用或者忽略的模块错误可以忽略。

yum install readline-devel sqlite-devel bzip2-devel openssl-devel gdbm gdbm-devel libdbi-devel ncurses-libs zlib-devel


3. 查看 python 版本信息

[root@vip Python-2.7.14]# python -V
Python 2.6.6# 版本依旧是 2.6.6

4. 用 python2.7 替换旧版本

[root@vip Python-2.7.14]# cd /usr/bin/
[root@vip bin]# ls python* -l   # 旧 python 版本信息
-rwxr-xr-x. 2 root root 4864 2月  22 2013  python
lrwxrwxrwx. 1 root root    6 10月 22 18:38 python2 -> python
-rwxr-xr-x. 2 root root 4864 2月  22 2013  python2.6
[root@vip bin]# mv /usr/bin/python /usr/bin/python2.6.6
[root@vip bin]# ln -s /usr/local/python27/bin/python2.7 /usr/bin/python
[root@vip bin]# ln -s /usr/local/python27/bin/python2.7 /usr/bin/python2.7
[root@vip bin]# ls python* -l
lrwxrwxrwx. 1 root root   33 10月 23 00:01 python -> /usr/local/python27/bin/python2.7
lrwxrwxrwx. 1 root root    6 10月 22 18:38 python2 -> python
-rwxr-xr-x. 2 root root 4864 2月  22 2013 python2.6
-rwxr-xr-x. 2 root root 4864 2月  22 2013 python2.6.6

5. 重新验证 Python 版本信息

[root@vip bin]# python -V
Python 2.7.14

可以看到,系统识别的 python 版本已经是 python 2.7.14

执行 python -V 遇到的问题:

python: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory
# 原因:linux系统默认没有把/usr/local/python27/lib路径加入动态库搜索路径

解决:

[root@vip ~]# vim /etc/ld.so.conf
# 添加如下一行内容
/usr/local/python27/lib
[root@vip ~]# ldconfig  # 使新添加的路径生效

 

二、解决 yum 兼容性问题

因为 yum 是不兼容 Python 2.7 的,所以 yum 不能正常工作,我们需要指定 yum 的 Python 为 2.6。

1. 升级 python 后 yum 出现的问题

[root@vip bin]# yum 
There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:
 No module named yum
... ... ... ...

2. 编辑 yum 配置文件

[root@vip bin]# vim /usr/bin/yum
#!/usr/bin/python
# 第一行修改为 python2.6.6
#!/usr/bin/python2.6.6

3. 验证 yum 问题解决

[root@vip bin]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
... ... ... ...

 

三、升级 python 后,安装 pip 工具

1. 下载安装

[root@vip ~]# wget https://bootstrap.pypa.io/get-pip.py
[root@vip ~]# python get-pip.py

2. 设置软连接

[root@vip ~]# ln -s /usr/local/python27/bin/pip2.7 /usr/bin/pip

 

四、安装 ipython

[root@vip ~]# pip install ipython==1.2.1
[root@vip ~]# ln -s /usr/local/python27/bin/ipython /usr/bin/ipython