centos 7升级Python2.7至Python3.8



Python 2.7.5已于2020年初停止支持,而在centos 7中默认安装的是Python2.7.5,因此需要自己手动替换

1:查看当前python版本

[root@centos Python-3.6.1]# python
Python 2.7.5 (default, Nov  6 2016, 00:28:07) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

以上输出Python 2.7.5表示当前版本,因为yum使用的是Python2.7因此不能删除,所以只能重新安装一个替换版本。
输入以下命令查看目前系统使用的Python以及安装目录,接下来将要对该目录下的Python进行修改

[root@centos ~]# which python
/usr/bin/python
[root@centos ~]# ll /usr/bin/python*
lrwxrwxrwx. 1 root root    7 May 30 05:52 /usr/bin/python -> python2
lrwxrwxrwx. 1 root root    9 May 30 05:52 /usr/bin/python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 Nov  6  2016 /usr/bin/python2.7

2:准备工作

首先安装以下组件,文后会有具体说明。若未安装则会在中途出现错误。

yum -y install gcc openssl-devel 

3:下载安装Python3.8.3

官网下载需要的Python版本,此处以Python3.8.3为例

wget https://www.python.org/ftp/python/3.8.3/Python-3.8.3.tgz
tar xvf Python-3.8.3.tgz
mkdir /usr/local/python3
cd Python-3.8.3
./configure --prefix=/usr/local/python3
make
make install

4:检测是否成功

[root@centos ~]# /usr/local/python3/bin/python3.8
Python 3.8.3 (default, May 26 2020, 12:35:02)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.

显示Python 3.8.3已安装成功并已可用。

5:替换旧Python

我们已经确认当前使用Python为2.7.5并且在/usr/bin目录下,接下来需要做的就是替换/usr/bin/python,并更改yum使用的解释器,不然yum将无法使用
重命名当前Python,并将Python3.8.3软链接至/usr/bin/python,建好后查看已经存在,并指向正确

mv /usr/bin/python /usr/bin/python.bak
ln -s /usr/local/python3/bin/python3.8 /usr/bin/python


验证是否成功,直接输入python命令进行验证

[root@centos ~]# python
Python 3.8.3 (default, May 26 2020, 12:35:02)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.

6:解决升级Python后yum,Firewall等不能用的问题

我们需要将以下两个文件的抬头标识修改为原先的Python2.7版本
vi /user/bin/yum
vi /usr/libexec/urlgrabber-ext-down

将#! /usr/bin/python2修改为
#! /usr/bin/python2.7 即可

firewalld防火墙无法启动的情况下亦同上,修改以下三个文件的头部内容即可。
/usr/bin/firewall-cmd
firewall-offline-cmd
/usr/sbin/firewalld

测试一下结果

[root@centos ~]# systemctl start firewalld.service 
[root@centos ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2020-05-26 20:40:38 UTC; 7h left
     Docs: man:firewalld(1)
 Main PID: 298 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─298 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid

May 26 20:40:36 vps170872281.diahosting.com systemd[1]: Starting firewalld - dynamic firewall daemon...
May 26 20:40:38 vps170872281.diahosting.com systemd[1]: Started firewalld - dynamic firewall daemon.
May 26 20:40:38 vps170872281.diahosting.com firewalld[298]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure configuration option. It will be re...g it now.
Hint: Some lines were ellipsized, use -l to show in full.

没有问题,已可以正常使用。

7:检查其它可能存在的与原Python版本有关的文件

除此之外还有一些其他文件使用的Python,可以使用grep命令查看哪些文件使用了Python解释器
根据具体情况和需要进行修改。
以下方法查看/usr/bin目录下与python有关的文件

[root@centos ~]# grep -ai /usr/bin/python /usr/bin/*
/usr/bin/chardetect:#!/usr/bin/python
/usr/bin/debuginfo-install:#!/usr/bin/python
/usr/bin/find-repos-of-install:#!/usr/bin/python -tt
/usr/bin/firewall-cmd:#!/usr/bin/python2.7 -Es
/usr/bin/firewall-offline-cmd:#!/usr/bin/python2.7 -Es
/usr/bin/msghack:#!/usr/bin/python
/usr/bin/needs-restarting:#!/usr/bin/python -tt
/usr/bin/package-cleanup:#!/usr/bin/python
/usr/bin/pydoc:#!/usr/bin/python2.7
/usr/bin/repo-graph:#!/usr/bin/python
/usr/bin/repo-rss:#!/usr/bin/python
/usr/bin/repoclosure:#!/usr/bin/python -t
/usr/bin/repodiff:#!/usr/bin/python -tt
/usr/bin/repomanage:#!/usr/bin/python
/usr/bin/repoquery:#!/usr/bin/python -tt
/usr/bin/reposync:#!/usr/bin/python -tt
/usr/bin/repotrack:#!/usr/bin/python -tt
/usr/bin/show-changed-rco:#! /usr/bin/python -tt
/usr/bin/show-changed-rco:#!/usr/bin/python -tt
/usr/bin/show-installed:#!/usr/bin/python
/usr/bin/system-config-firewall-tui:#!/usr/bin/python
/usr/bin/systemd-sysv-convert:#!/usr/bin/python
/usr/bin/urlgrabber:#!/usr/bin/python -t
/usr/bin/verifytree:#!/usr/bin/python -tt
/usr/bin/yum:#!/usr/bin/python2.7
/usr/bin/yum-builddep:#!/usr/bin/python -tt
/usr/bin/yum-config-manager:#!/usr/bin/python -tt
/usr/bin/yum-debug-dump:#!/usr/bin/python -tt
/usr/bin/yum-debug-restore:#!/usr/bin/python -tt
/usr/bin/yum-groups-manager:#!/usr/bin/python -tt
/usr/bin/yumdownloader:#!/usr/bin/python

8:中途可能出现的错误

进行configure 安装时,可能会出现以下错误提示则表示系统没有安装gcc
configure: error: no acceptable C compiler found in $PATH 执行

yum -y install gcc


make install过程中出现以下错误提示:
FAIL: test_with_pip (test.test_venv.EnsurePipTest)
zipimport.ZipImportError: can't decompress data; zlib not available
出现这种错误一般因为Python的安装环境没有安装好,安装以下软件

yum -y install openssl-devel

如果还是有报错则安装Development tools

yum groupinstall Development tools

安装成功后make clean以下,再重新进行make install即可。

最后更改: in 2020/05/26 21:28