Centos7.5 从Python2.7.5升级到Python2.7.16
Centos7 默认安装的python版本是2.7.5,由于某些平台提示建议更新版本,防止跨脚本攻击,所以现对其进行升级,更新至2.7.16版本(截至目前2019-8-12的最新版本)。
查看当前python版本
[root@csensix ~]# python -V
运行结果:Python 2.7.5
下载并安装最新版本 Python 2.7.16
下载
.tar.xz
或 .tgz
压缩版本二选一,这里我下载的是体积较小的 .tar.xz
版本。
# tar.xz 版本
[root@csensix ~]# wget https://www.python.org/ftp/python/2.7.16/Python-2.7.16.tar.xz
# tgz 版本
[root@csensix ~]# wget https://www.python.org/ftp/python/2.7.16/Python-2.7.16.tgz
解压
[root@csensix ~]# tar -xf Python-2.7.16.tar.xz
# 或者分两步
[root@csensix ~]# xz -d Python-2.7.16.tar.xz
[root@csensix ~]# tar xvf Python-2.7.16.tar
编译安装
[root@csensix ~]# cd Python-2.7.16
[root@csensix ~]# ./configure --prefix=/usr/local
[root@csensix ~]# make
[root@csensix ~]# make altinstall
为避免覆盖系统自带python,故没有选择常规的make install
,而是使用了 make altinstall
。
修改默认python版本
安装完之后执行 python -V
,此时显示的还是老的 2.7.5
版本,因为 /usr/bin/python
指向的还是原来的版本,所以需要做相应的更改。
首先,备份老版本
[root@csensix ~]# mv /usr/bin/python /usr/bin/python2.7.5
链接新版本
[root@csensix ~]# ln -s /usr/local/bin/python2.7 /usr/bin/python
至此,新版本安装基本完成。现在,python -V
显示的结果是 Python 2.7.16
,如果希望访问老版本,那么执行 python2.7.5
即可,如下:
[root@csensix ~]# python2.7.5
Python 2.7.5 (default, Apr 11 2018, 07:36:10)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
更新yum和pip
更新yum
由于 yum
默认使用的还是老版本(2.7.5
),所以执行时会报错,如:
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
Please install a package which provides this module, or
verify that the module is installed correctly.
It's possible that the above module doesn't match the
current version of Python, which is:
2.7.16 (default, Aug 12 2019, 09:10:53)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
If you cannot solve this problem yourself, please go to
the yum faq at:
http://yum.baseurl.org/wiki/Faq
此时,只需要更改 /usr/bin/yum
文件第一行python
版本即可。
[root@csensix ~]# vim /usr/bin/yum
首行的#!/usr/bin/python 改为 #!/usr/bin/python2.7.5
如果执行yum
还报错,如
ImportError: No module named urlgrabber.grabber
修改 /usr/libexec/urlgrabber-ext-down
使用的python版本
[root@csensix ~]# vim /usr/libexec/urlgrabber-ext-down
首行的#!/usr/bin/python 改为 #!/usr/bin/python2.7.5
更新 pip
升级python版本之后,很容易造成pip不可用,需要重新安装pip。
下载安装setuptools
[root@csensix ~]# wget https://files.pythonhosted.org/packages/1d/64/a18a487b4391a05b9c7f938b94a16d80305bf0369c6b0b9509e86165e1d3/setuptools-41.0.1.zip
[root@csensix ~]# unzip setuptools-41.0.1.zip
[root@csensix ~]# cd setuptools-41.0.1
[root@csensix ~]# python setup.py install
下载安装pip
[root@csensix ~]# wget https://files.pythonhosted.org/packages/aa/1a/62fb0b95b1572c76dbc3cc31124a8b6866cbe9139eb7659ac7349457cf7c/pip-19.2.2.tar.gz
[root@csensix ~]# tar zxvf pip-19.2.2.tar.gz
[root@csensix ~]# cd pip-19.2.2
[root@csensix ~]# python setup.py install
[root@csensix ~]# mv /usr/bin/pip /usr/bin/pip.bak
[root@csensix ~]# ln -s /usr/local/python27/bin/pip2.7 /usr/bin/pip
至此,pip更新完毕。用命令 pip list
可测试pip功能是否正常。
ps:如果需要使用sqlite3,需要安装sqlite模块 yum install -y sqlite-devel