Centos7.8误删Python2.7之后,导致yum和Python命令无法使用

Centos7.8误删Python2.7之后,导致yum和Python命令无法使用

先简单介绍下我的情况与背景:

我在昨天写一个模块,跑Python脚本报错,由于我不熟习Python2,3之间语法有差异,导致脚本一直跑步起来,后面安装了Python3, 由于我个人安装了软件新版本一般会卸载旧版本以节约空间,而yum模块采用Python2编写,导致使用yum命令会一直报错:

需要提前说明的是,如果你只是安装了Python3并没有删除Python2的话,那只需要去yum配置文件里将默认编译器指向Python2即可,后面也会给出解决方案。

情况一报错日志

若安装了Python3且没有删除Python2,那么启动yum命令会报错:

File "/usr/bin/yum", line 30
    except KeyboardInterrupt, e:
                            ^
SyntaxError: invalid syntax

情况一报错原因

这种情况的原因是yum配置文件解释器默认是/usr/bin/python,而安装Python3之后教程里一般会将这个软连接指向Python3所在的目录,也就是程序引用了错误的Python版本解释器,解决办法就将yum配置文件解释器指向原来的Python2版本:

 # 需要改 /usr/bin/yum 和 /usr/libexec/urlgrabber-ext-down ,改法一样,这儿以第一个为例
 vim /usr/bin/yum  
#!/usr/bin/python  # 修改之前
import sys
try:
    import yum
except ImportError:
    print >> sys.stderr, """\
There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:
#!/usr/bin/python2.7  # 修改之后
import sys
try:
    import yum
except ImportError:
    print >> sys.stderr, """\
There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:

当然如果你之前的Python2版本不是2.7那可以通过 whereis python查看

[root@node02 local]# whereis  python
python: /usr/bin/python /usr/bin/python2.7 /usr/lib/python2.7 /usr/lib64/python2.7 /etc/python /usr/include/python2.7 /usr/share/man/man1/python.1.gz

如果你是像我一样更复杂的场景,也就是将Python2误删了,那处理起来相对来说麻烦一些

情况二报错日志

我删除Python2之后重新安装了Python2,并且根据另一个虚拟机节点建立了软连接,这次启动yum不报错,但是出现下面的信息提示:

$ yum install
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.5 (default, Dec 25 2022, 22:37:27) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]

If you cannot solve this problem yourself, please go to 
the yum faq at:
  http://yum.baseurl.org/wiki/Faq

情况二解决办法

这个方法就是根据rpm包的方式安装,需要说明的我这个方法不一定是最好的, 但是时间有限, 已经耗费了大半天时间, 且即使是这种安装方式依旧会有坑,例如网上的教程大多是去网易的镜像地址去下载对应的rpm包,我依次下载之后到本地依旧是依赖报错,解决办法如下:

在开始之前需要知道你的系统版本以及位数:

[root@node02 local]# lsb_release -a  # 没安装这个包,我在另一个节点 yum install redhat-lsb -y查看
LSB Version:	:core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-n
oarchDistributor ID:	CentOS
Description:	CentOS Linux release 7.8.2003 (Core)
Release:	7.8.2003
Codename:	Core
[root@node02 local]# getconf LONG_BIT
64

7.8.2003 在下面的路径中会用到,x86_64是指指令集,后面的包名依据系统版本不同会有所不同,可以复制名称去

https://vault.centos.org/7.8.2003/os/x86_64/Packages/ 下查找最相近的,新建文件夹后需要下载的详细清单如下,如果你的系统版本以及位数正好与我相同,可以点击点击下载

mkdir py_rpm  # 创建文件夹
cd  py_rpm    # 进入文件夹
# 复制下面的命令去命令行执行
wget https://vault.centos.org/7.8.2003/os/x86_64/Packages/lvm2-python-libs-2.02.186-7.el7.x86_64.rpm
wget https://vault.centos.org/7.8.2003/os/x86_64/Packages/libxml2-python-2.9.1-6.el7.4.x86_64.rpm
wget https://vault.centos.org/7.8.2003/os/x86_64/Packages/python-libs-2.7.5-88.el7.x86_64.rpm
wget https://vault.centos.org/7.8.2003/os/x86_64/Packages/python-ipaddress-1.0.16-2.el7.noarch.rpm
wget https://vault.centos.org/7.8.2003/os/x86_64/Packages/python-backports-1.0-8.el7.x86_64.rpm
wget https://vault.centos.org/7.8.2003/os/x86_64/Packages/python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarch.rpm
wget https://vault.centos.org/7.8.2003/os/x86_64/Packages/python-2.7.5-88.el7.x86_64.rpm
wget https://vault.centos.org/7.8.2003/os/x86_64/Packages/python-iniparse-0.4-9.el7.noarch.rpm
wget https://vault.centos.org/7.8.2003/os/x86_64/Packages/python-pycurl-7.19.0-19.el7.x86_64.rpm
wget https://vault.centos.org/7.8.2003/os/x86_64/Packages/python-urlgrabber-3.10-10.el7.noarch.rpm
wget https://vault.centos.org/7.8.2003/os/x86_64/Packages/python-setuptools-0.9.8-7.el7.noarch.rpm
wget https://vault.centos.org/7.8.2003/os/x86_64/Packages/python-kitchen-1.1.1-5.el7.noarch.rpm
wget https://vault.centos.org/7.8.2003/os/x86_64/Packages/python-chardet-2.2.1-3.el7.noarch.rpm
wget https://vault.centos.org/7.8.2003/os/x86_64/Packages/rpm-python-4.11.3-43.el7.x86_64.rpm
wget https://vault.centos.org/7.8.2003/os/x86_64/Packages/yum-utils-1.1.31-53.el7.noarch.rpm
wget https://vault.centos.org/7.8.2003/os/x86_64/Packages/yum-3.4.3-167.el7.centos.noarch.rpm
wget https://vault.centos.org/7.8.2003/os/x86_64/Packages/yum-metadata-parser-1.1.4-10.el7.x86_64.rpm
wget https://vault.centos.org/7.8.2003/os/x86_64/Packages/yum-plugin-aliases-1.1.31-53.el7.noarch.rpm
wget https://vault.centos.org/7.8.2003/os/x86_64/Packages/yum-plugin-protectbase-1.1.31-53.el7.noarch.rpm
wget https://vault.centos.org/7.8.2003/os/x86_64/Packages/yum-plugin-fastestmirror-1.1.31-53.el7.noarch.rpm

执行下面的命令安装:

rpm -Uvh --replacepkgs lvm2-python-libs*.rpm --nodeps --force
rpm -Uvh --replacepkgs libxml2-python*.rpm --nodeps --force
rpm -Uvh --replacepkgs python*.rpm --nodeps --force
rpm -Uvh --replacepkgs rpm-python*.rpm yum*.rpm --nodeps --force
$ rpm -Uvh --replacepkgs rpm-python*.rpm yum*.rpm --nodeps --force
准备中...                          ################################# [100%]
正在升级/安装...
   1:yum-metadata-parser-1.1.4-10.el7 ################################# [ 14%]
   2:rpm-python-4.11.3-43.el7         ################################# [ 29%]
   3:yum-plugin-fastestmirror-1.1.31-5################################# [ 43%]
   4:yum-3.4.3-167.el7.centos         ################################# [ 57%]
   5:yum-utils-1.1.31-53.el7          ################################# [ 71%]
   6:yum-plugin-aliases-1.1.31-53.el7 ################################# [ 86%]
   7:yum-plugin-protectbase-1.1.31-53.################################# [100%]

检验

$ python
Python 2.7.5 (default, Apr  2 2020, 13:16:51) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

$  yum install redhat-lsb -y

大功告成 !

posted @ 2022-12-26 12:10  等不到的口琴  阅读(1603)  评论(0编辑  收藏  举报