likes
comments
collection
share

CentOS7误删自带python2或yum异常导致yum命令不可用的解决方法

作者站长头像
站长
· 阅读数 23

异常场景1

在执行yum命令时,系统无法找到 Python 解释器,因此无法运行 yum 命令,具体异常如下:

 yum update -y
 
-bash: /usr/bin/yum: /usr/bin/python: 坏的解释器: 没有那个文件或目录

原因

这种情况通常出现在升级或安装Python3版本之后,导致python2失效了,或者说python2被卸载了,但是系统中某些组件依赖于旧版本的 Python

检查/usr/bin/python是否存在,如果不存在,则需要安装Python解释器。否则就是存在,但是不是正确的版本,请将其链接到正确的 Python解释器即可

验证

查看/usr/bin/yum文件内容

#!/usr/bin/python
import sys
try:
    import yum
except ImportError:

发现yum服务使用的默认python版本是python2。接着使用命令查看,发现已经没有python文件,那yum文件中指定的/usr/bin/python自然就无效了

[root@administrator .virtualenvs]# ls /usr/bin/python*
/usr/bin/python3

解决方案

注意:由于yum服务默认使用的python2,不能改成python3,以防出现服务依然不可用情况,所以只能安装一个python2

wget https://www.python.org/ftp/python/2.7.7/Python-2.7.7.tgz

cd Python-2.7.7

./configure

make && make install

让Python2与Python3共存

Python2安装好后,查看/usr/bin/目录下是否有python与pip(应该是有,事后Blog记录),没有则手动建立软链接

[root@administrator ~]# ll /usr/bin/python*
lrwxrwxrwx 1 root root    7 37 15:07 /usr/bin/python -> python2
lrwxrwxrwx 1 root root    9 37 15:07 /usr/bin/python2 -> python2.7
-rwxr-xr-x 1 root root 7144 628 2022 /usr/bin/python2.7
lrwxrwxrwx 1 root root   30 37 10:32 /usr/bin/python3 -> /usr/local/python3/bin/python3
[root@administrator ~]# ll /usr/bin/pip*
-rwxr-xr-x 1 root root 221 37 16:10 /usr/bin/pip
-rwxr-xr-x 1 root root 221 37 16:10 /usr/bin/pip2
-rwxr-xr-x 1 root root 221 37 16:10 /usr/bin/pip2.7
-rwxr-xr-x 1 root root 237 37 10:31 /usr/bin/pip3

以建立Python3和pip3的软链接为例:

ln -s /usr/local/python2/bin/python2 /usr/bin/python3

ln -s /usr/local/python2/bin/pip /usr/bin/pip3

添加环境变量配置vim /etc/profile,然后使配置生效:source /etc/profile

export PYTHON_HOME=/usr/local/python3/bin

export PATH=$PATH:$PYTHON_HOME/bin

检查Python3及pip3是否正常

[root@master ~]# python3 -V
Python 3.9.11

[root@master ~]# pip3 -V
pip 23.0.1 from /usr/local/python3/lib/python3.9/site-packages/pip (python 3.9)

最后验证 Python2与Python3共存

[root@administrator ~]# python
Python 2.7.5 (default, Jun 28 2022, 15:30:04) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

[root@administrator ~]# python3
Python 3.9.11 (main, Mar  7 2023, 10:30:01) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
[root@administrator ~]# pip -V
pip 20.3.4 from /usr/lib/python2.7/site-packages/pip (python 2.7)

[root@administrator ~]# pip3 -V
pip 23.0.1 from /usr/local/python3/lib/python3.9/site-packages/pip (python 3.9)

异常场景2

执行yum命令出现如下异常提示

[root@administrator ~]# yum
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.6.6 (r266:84292, Mar  7 2023, 14:15:28) 
[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

原因

出现这个异常,原因不明,猜测:

1.缺少python2异常

 2.yum软件异常

解决方案

先卸载、删除yum残留文件后,从阿里镜像仓库下载Centos7对应系统版本的yum包,然后进行安装

卸载、删除残留文件

强制删除python已安装程序及其关联

rpm -qa|grep python|xargs rpm -ev --allmatches --nodeps

删除残余python文件

whereis python |xargs rm -frv

验证删除,返回为无结果

whereis python

删除现有yum

rpm -qa|grep yum|xargs rpm -ev --allmatches --nodeps

删除残余yum文件

whereis yum |xargs rm -frv

下载系统版本对应的yum包

查看系统信息

[root@administrator ~]# lsb_release -a
LSB Version:    :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description:    CentOS Linux release 7.9.2009 (Core)
Release:        7.9.2009
Codename:       Core

下载的包的版本应与系统版本号对应的一致

https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/

CentOS7误删自带python2或yum异常导致yum命令不可用的解决方法 搜索关键字:yum,找到yum-3.4.3-168.el7.centos.noarch.rpm CentOS7误删自带python2或yum异常导致yum命令不可用的解决方法 下载

wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/yum-3.4.3-168.el7.centos.noarch.rpm

执行安装yum,提示缺少相关依赖

[root@administrator ~]# rpm -ivh yum-3.4.3-168.el7.centos.noarch.rpm
error: Failed dependencies:
        /usr/bin/python is needed by yum-3.4.3-168.el7.centos.noarch
        python >= 2.4 is needed by yum-3.4.3-168.el7.centos.noarch
        python(abi) = 2.7 is needed by yum-3.4.3-168.el7.centos.noarch
        python-iniparse is needed by yum-3.4.3-168.el7.centos.noarch
        python-sqlite is needed by yum-3.4.3-168.el7.centos.noarch
        python-urlgrabber >= 3.10-8 is needed by yum-3.4.3-168.el7.centos.noarch
        rpm-python is needed by yum-3.4.3-168.el7.centos.noarch
        yum-metadata-parser >= 1.1.0 is needed by yum-3.4.3-168.el7.centos.noarch
        yum-plugin-fastestmirror is needed by yum-3.4.3-168.el7.centos.noarch

根据提示信息,将第一个依赖关键词python 复制到镜像网站查询获取下载地址 CentOS7误删自带python2或yum异常导致yum命令不可用的解决方法 下载依赖包

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

执行安装,同时发现又提示依赖

[root@administrator ~]# rpm -ivh python-2.7.5-89.el7.x86_64.rpm 
error: Failed dependencies:
        libpython2.7.so.1.0()(64bit) is needed by python-2.7.5-89.el7.x86_64
        python-libs(x86-64) = 2.7.5-89.el7 is needed by python-2.7.5-89.el7.x86_64

此时将提示缺少的依赖复制到镜像网站查询并下载,注意版本 CentOS7误删自带python2或yum异常导致yum命令不可用的解决方法 安装依赖

rpm -ivh python-libs-2.7.5-89.el7.x86_64.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
   1:python-libs-2.7.5-89.el7         ################################# [100%]

以此类推,一步一步安装所需要的依赖。

当依赖的包无法安装时,暂时不管,先下载下来。

当所有依赖都下载后,能安装的都安装了,最后一次性安装

使用–nodeps --force参数不考虑依赖包,强制安装

[root@administrator ~]# rpm -ivh *.rpm --nodeps --force
Preparing...                          ################################# [100%]
Updating / installing...
   1:rpm-4.11.3-45.el7                ################################# [ 25%]
   2:rpm-python-4.11.3-45.el7         ################################# [ 50%]
   3:yum-plugin-fastestmirror-1.1.31-5################################# [ 75%]
   4:yum-3.4.3-168.el7.centos         ################################# [100%]

完成后,怀着忐忑的请求执行yum命令,特么的成功了!

root@administrator ~]# yum
Configuration file /etc/yum/pluginconf.d/product-id.conf not found
Unable to find configuration file for plugin product-id
Configuration file /etc/yum/pluginconf.d/search-disabled-repos.conf not found
Unable to find configuration file for plugin search-disabled-repos
Configuration file /etc/yum/pluginconf.d/subscription-manager.conf not found
Unable to find configuration file for plugin subscription-manager
Loaded plugins: fastestmirror
You need to give some command
Usage: yum [options] COMMAND

List of Commands:

check          Check for problems in the rpmdb
check-update   Check for available package updates
clean          Remove cached data
deplist        List a package's dependencies
distribution-synchronization Synchronize installed packages to the latest available versions
downgrade      downgrade a package
erase          Remove a package or packages from your system
fs             Acts on the filesystem data of the host, mainly for removing docs/lanuages for minimal hosts.
fssnapshot     Creates filesystem snapshots, or lists/deletes current snapshots.
groups         Display, or use, the groups information
help           Display a helpful usage message
history        Display, or use, the transaction history
info           Display details about a package or group of packages
install        Install a package or packages on your system
list           List a package or groups of packages
load-transaction load a saved transaction from filename