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

昨天本来想部署个东西,是基于Python3运行的,后面发现自己装的python3版本太低就手动更新了下,没有做备份,然后手贱把系统自带的python2也删了,结果Python2报错了,pip也报错,连yum都不能用了,提示

1
-bash: /usr/bin/yum: /usr/bin/python

手动编译安装了python2后,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.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

我想这不是死定了,不会要重装系统吧,后面群里的大佬丢了个解决方案给我,我就跟着操作下,怀着忐忑的心敲下yum,然后发现yum好了。
我看了下有两个场景,我遇到的问题适合场景二,但是这里我都蛮记录下。

一、异常场景一

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

1
2
3
yum update -y

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

1、原因

这种情况通常出现在升级或安装Python3版本之后,导致python2失效了,或者说python2被卸载了,但是系统中某些组件依赖于旧版本的 Python
检查/usr/bin/python是否存在,如果不存在,则需要安装Python解释器。否则就是存在,但是不是正确的版本,请将其链接到正确的 Python解释器即可

2、验证

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

1
2
3
4
5
#!/usr/bin/python
import sys
try:
import yum
except ImportError:

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

1
2
[root@administrator]# ls /usr/bin/python*
/usr/bin/python3
3、解决方案

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

1
2
3
4
5
6
7
wget https://www.python.org/ftp/python/2.7.7/Python-2.7.7.tgz

cd Python-2.7.7

./configure

make && make install

4、让Python2与Python3共存

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

1
2
3
4
5
[root@administrator ~]# ll /usr/bin/python*
lrwxrwxrwx 1 root root 7 3月 7 15:07 /usr/bin/python -> python2
lrwxrwxrwx 1 root root 9 3月 7 15:07 /usr/bin/python2 -> python2.7
-rwxr-xr-x 1 root root 7144 6月 28 2022 /usr/bin/python2.7
lrwxrwxrwx 1 root root   30 3月   7 10:32 /usr/bin/python3 -> /usr/local/python3/bin/python3

1
2
3
4
5
[root@administrator ~]# ll /usr/bin/pip*
-rwxr-xr-x 1 root root 221 3月 7 16:10 /usr/bin/pip
-rwxr-xr-x 1 root root 221 3月 7 16:10 /usr/bin/pip2
-rwxr-xr-x 1 root root 221 3月 7 16:10 /usr/bin/pip2.7
-rwxr-xr-x 1 root root 237 3月   7 10:31 /usr/bin/pip3

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

1
2
3
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

1
2
3
export PYTHON_HOME=/usr/local/python3/bin

export PATH=$PATH:$PYTHON_HOME/bin

检查Python3及pip3是否正常

1
2
3
4
5
[root@master ~]# python3 -V
Python 3.9.11

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

最后验证 Python2与Python3共存

1
2
3
4
5
6
7
8
9
10
11
[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.
>>>

1
2
3
4
5
[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)

二、异常场景二

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[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、原因

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

1
2
3
1.缺少python2异常

2.yum软件异常

2、解决方案

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

3、卸载、删除残留文件

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

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

删除残余python文件

1
whereis python |xargs rm -frv

验证删除,返回为无结果

1
whereis python

删除现有yum

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

下载系统版本对应的yum包

1
2
3
[root@administrator ~]# cat /etc/redhalt-release

CentOS Linux release 7.9.2009 (Core)

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

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

repo

搜索关键字:yum,找到 yum-3.4.3-168.el7.centos.noarch.rpm

repo

下载

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

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

1
2
3
4
5
6
7
8
9
10
11
[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 复制到镜像网站查询获取下载地址,比如 python 、 python(abi) 、 python-iniparse

repo

下载依赖包

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

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

1
2
3
4
[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

此时将提示缺少的依赖复制到镜像网站查询并下载,注意版本

repo

安装依赖

1
2
3
4
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参数不考虑依赖包,强制安装

1
2
3
4
5
6
7
[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命令,成功了!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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

总结

场景一就是通过编译安装,然后软链的方式来区分python3和python2来达到共存的目的。
场景二就是清除所有Pyhon的程序、关联文件和yum的残留文件,然后下载对应的yum rpm文件和yum依赖的rpm文件来达到恢复yum程序的目的。

微信扫一扫关注我吧

戴戴的Linux 戴戴的Linux

文章目录
  1. 1. 一、异常场景一
    1. 1.1. 1、原因
    2. 1.2. 2、验证
    3. 1.3. 3、解决方案
    4. 1.4. 4、让Python2与Python3共存
  2. 2. 二、异常场景二
    1. 2.1. 1、原因
    2. 2.2. 2、解决方案
    3. 2.3. 3、卸载、删除残留文件
  3. 3. 总结
  4. 4. 微信扫一扫关注我吧


本站总访问量 本文总阅读量