1、yum命令不能使用

【1】报错

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】问题分析

大部分人出现这个错误的原因在于拿到服务器的第一时间发现python版本是2.x,想升级到python3 导致yum损坏,博主花了两个小时查阅资料,最终总算解决问题。


2、完全删除python及yum重新安装

【1】删除python
1
2
3
4
5
6
7
8
9
#删除现有的python
##强制删除已安装程序及其关联
[root@test ~] rpm -qa|grep python|xargs rpm -ev --allmatches --nodeps

##删除所有残余文件 ##xargs,允许你对输出执行其他某些命令
[root@test ~] whereis python |xargs rm -frv

##验证删除,返回无结果
[root@test ~] whereis python

【2】删除yum
1
2
3
#删除现有的yum
[root@test ~] rpm -qa|grep yum|xargs rpm -ev --allmatches --nodeps
[root@test ~] whereis yum |xargs rm -frv

从中科,163或者阿里云镜像下载相对应的包

centos版本可以根据自己的版本去找对应Packages
如果不知道自己的系统版本,命令行输入这个cat /etc/redhat-release


【3】下载python依赖rpm包
1
2
3
4
5
6
7
#下载Python安装包
python-libs-2.7.5-48.el7.x86_64.rpm #被python依赖
python-2.7.5-48.el7.x86_64.rpm
python-iniparse-0.4-9.el7.noarch.rpm #被yum依赖
python-pycurl-7.19.0-19.el7.x86_64.rpm #被python-urlgrabber依赖
python-urlgrabber-3.10-8.el7.noarch.rpm #被yum依赖
rpm-python-4.11.3-21.el7.x86_64.rpm #被yum依赖

选用阿里地址下载,访问阿地址搜索相关的包

1
2
3
4
5
6
7
8
9
10
11
12
#下载python包
wget https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/python-libs-2.7.5-89.el7.x86_64.rpm

wget https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/python-2.7.5-89.el7.x86_64.rpm

wget https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/python-iniparse-0.4-9.el7.noarch.rpm

wget https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/python-pycurl-7.19.0-19.el7.x86_64.rpm

wget https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/python-urlgrabber-3.10-10.el7.noarch.rpm

wget https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/rpm-python-4.11.3-45.el7.x86_64.rpm

【4】下载yum依赖rpm包
1
2
3
4
#yum安装包列表:
yum-3.4.3-150.el7.centos.noarch.rpm, 就是它依赖了上面的python库
yum-metadata-parser-1.1.4-10.el7.x86_64.rpm
yum-plugin-fastestmirror-1.1.31-40.el7.noarch.rpm
1
2
3
4
5
6
#下载yum包
mkdir -p yum-rpm/
cd yum-rpm/
wget https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/yum-3.4.3-168.el7.centos.noarch.rpm
wget https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/yum-metadata-parser-1.1.4-10.el7.x86_64.rpm
wget https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/yum-plugin-fastestmirror-1.1.31-54.el7_8.noarch.rpm

【5】强制安装python
1
2
3
4
5
6
7
#强制安装python(--nodeps表示强制)
rpm -Uvh python-libs-2.7.5-89.el7.x86_64.rpm --nodeps
rpm -Uvh python-2.7.5-89.el7.x86_64.rpm --nodeps
rpm -Uvh python-iniparse-0.4-9.el7.noarch.rpm --nodeps
rpm -Uvh python-pycurl-7.19.0-19.el7.x86_64.rpm --nodeps
rpm -Uvh python-urlgrabber-3.10-10.el7.noarch.rpm --nodeps
rpm -Uvh rpm-python-4.11.3-45.el7.x86_64.rpm --nodeps

【6】强制安装yum
1
2
3
4
5
#强制安装yum(--nodeps表示强制)
cd yum-rpm/
rpm -Uvh yum-3.4.3-168.el7.centos.noarch.rpm --nodeps
rpm -Uvh yum-metadata-parser-1.1.4-10.el7.x86_64.rpm --nodeps
rpm -Uvh yum-plugin-fastestmirror-1.1.31-54.el7_8.noarch.rpm --nodeps

【7】测试
1
yum install vim -y