No module named yum问题解决

解决CentOS升级/卸载Python后yum失效的问题


问题

升级或卸载系统自带的Python后会导致yum启动报错。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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.13 (default, Aug 11 2017, 15:02:41)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)]

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

这是因为yum依赖Python启动,当Python import yum时无法找到yum导致。因此需要为yum指定特定版本的Python(centos6为Python 2.6)


方法

删除剩余python和yum
1
2
rpm -aq|grep yum|xargs rpm -e –nodeps
rpm -aq|grep python|xargs rpm -e –nodeps
下载下列对应发行版本的rpm包(相应版本镜像site-packages里或者去阿里/网易源下载相应版本)
1
2
3
4
5
6
7
8
9
10
11
python-2.6.6-66.el6_8.x86_64.rpm
python-urlgrabber-3.9.1-11.el6.noarch.rpm
python-devel-2.6.6-66.el6_8.x86_64.rpm
rpm-python-4.8.0-55.el6.x86_64.rpm
python-iniparse-0.3.1-2.1.el6.noarch.rpm
yum-3.2.29-81.el6.centos.noarch.rpm
python-libs-2.6.6-66.el6_8.x86_64.rpm
yum-metadata-parser-1.1.2-16.el6.x86_64.rpm
python-pycurl-7.19.0-9.el6.x86_64.rpm
yum-plugin-fastestmirror-1.1.30-40.el6.noarch.rpm
python-setuptools-0.6.10-3.el6.noarch.rpm
进入下载目录安装rpm包(强制覆盖安装)
1
rpm -Uvh --replacepkgs ./*.rpm
检验

Python cli中import yum如果没有报如下错误则修复

1
2
3
4
5
6
7
8
[root@zxl ~]# python
Python 2.7.13 (default, Aug 11 2017, 15:02:41)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import yum
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named yum

多版本Python共存(以添加Python3为例)
  • 编译安装Python3(假设在/usr/local/python3.5)
  • /usr/bin目录下将python2、python2.6/python2.7、python3分别链接到相应的文件(本例中为/usr/local/python3.5/bin/python3.5)
  • /usr/bin/python链接的文件为默认的python版本
  • 注意:若将默认python版本链接为高版本,需要修改yum的启动脚本指定python版本vim /usr/bin/yum#!/usr/bin/python修改为#!/usr/bin/python2.6
  • 多版本pip共存方法同理

如果文章对您有帮助,感谢您的赞助支持!