No module named yum 错误处理

1. 问题背景

在安装Linux软件包的时候报错“ No module named yum”,导致yum命令无法正常使用,具体报错信息如下所示:

]$ yum install gcc git bc -y
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.18 (default, Apr 14 2023, 07:48:52)
[GCC 6.4.0]

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

2. 原因查找

由报错信息可知是python的版本和yum依赖的python版本不匹配,查看当前的python版本:

]$ python
Python 2.7.18 (default, Apr 14 2023, 07:48:52)
[GCC 6.4.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

发现当前的python版本是2.7.18,而通过redhat本地源安装的yum依赖的python版本应该是2.7.5,因此处理方法是修正python的版本为2.7.5。

3. 解决方法

比较简单直接的方法是下载所有的依赖rpm包,进行离线安装,下载方式可选择在一台联网的机器上操作:

]$ wget http://mirrors.163.com/centos/7/os/x86_64/Packages/python-2.7.5-34.el7.x86_64.rpm

采用上述方式下载以下所有依赖rpm包:

python-2.7.5-34.el7.x86_64.rpm
python-chardet-2.2.1-1.el7_1.noarch.rpm
python-devel-2.7.5-34.el7.x86_64.rpm
python-iniparse-0.4-9.el7.noarch.rpm
python-kitchen-1.1.1-5.el7.noarch.rpm
python-libs-2.7.5-34.el7.x86_64.rpm
python-pycurl-7.19.0-17.el7.x86_64.rpm
python-setuptools-0.9.8-4.el7.noarch.rpm
python-urlgrabber-3.10-7.el7.noarch.rpm
rpm-python-4.11.3-17.el7.x86_64.rpm
yum-3.4.3-132.el7.centos.0.1.noarch.rpm
yum-metadata-parser-1.1.4-10.el7.x86_64.rpm
yum-plugin-aliases-1.1.31-34.el7.noarch.rpm
yum-plugin-fastestmirror-1.1.31-34.el7.noarch.rpm
yum-plugin-protectbase-1.1.31-34.el7.noarch.rpm
yum-updateonboot-1.1.31-34.el7.noarch.rpm
yum-utils-1.1.31-34.el7.noarch.rpm

最后,将下载完成的所有rpm包上传至问题机器,通过下面的命令安装:

]# rpm -ivh ./* --nodeps --force
warning: ./dbus-python-devel-1.1.1-9.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:python-libs-2.7.5-89.el7         ################################# [  6%]
   2:python-2.7.5-89.el7              ################################# [ 12%]
   3:python-chardet-2.2.1-3.el7       ################################# [ 18%]
   4:python-kitchen-1.1.1-5.el7       ################################# [ 24%]
   5:python-iniparse-0.4-9.el7        ################################# [ 29%]
   6:python-pycurl-7.19.0-19.el7      ################################# [ 35%]
   7:python-urlgrabber-3.10-10.el7    ################################# [ 41%]
   8:rpm-python-4.11.3-45.el7         ################################# [ 47%]
   9:yum-metadata-parser-1.1.4-10.el7 ################################# [ 53%]
  10:yum-plugin-fastestmirror-1.1.31-5################################# [ 59%]
  11:yum-3.4.3-168.el7.centos         ################################# [ 65%]
  12:yum-utils-1.1.31-54.el7_8        ################################# [ 71%]
  13:yum-plugin-aliases-1.1.31-54.el7_################################# [ 76%]
  14:yum-plugin-protectbase-1.1.31-54.################################# [ 82%]
  15:yum-updateonboot-1.1.31-54.el7_8 ################################# [ 88%]
  16:python-setuptools-0.9.8-7.el7    ################################# [ 94%]
  17:dbus-python-devel-1.1.1-9.el7    ################################# [100%]

安装完成后,关闭当前会话,重新连接后验证python版本和yum命令。

]# python
Python 2.7.5 (default, Oct 14 2020, 14:45:30)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
]# yum install gcc git bc -y
Package gcc-4.8.5-36.el7.x86_64 already installed and latest version
Package bc-1.06.95-13.el7.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package git.x86_64 0:1.8.3.1-19.el7 will be installed
--> Processing Dependency: perl-Git = 1.8.3.1-19.el7 for package: git-1.8.3.1-19.el7.x86_64
--> Processing Dependency: perl(Error) for package: git-1.8.3.1-19.el7.x86_64
--> Processing Dependency: perl(Git) for package: git-1.8.3.1-19.el7.x86_64
--> Processing Dependency: perl(Term::ReadKey) for package: git-1.8.3.1-19.el7.x86_64
--> Running transaction check
---> Package perl-Error.noarch 1:0.17020-2.el7 will be installed
---> Package perl-Git.noarch 0:1.8.3.1-19.el7 will be installed
---> Package perl-TermReadKey.x86_64 0:2.30-20.el7 will be installed
--> Finished Dependency Resolution

命令恢复正常,可正常进行下载,问题解决。

你可能感兴趣的:(Linux,linux)