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

异常场景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/

在这里插入图片描述
搜索关键字:yum,找到yum-3.4.3-168.el7.centos.noarch.rpm
在这里插入图片描述
下载

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 复制到镜像网站查询获取下载地址
在这里插入图片描述
下载依赖包

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

此时将提示缺少的依赖复制到镜像网站查询并下载,注意版本
在这里插入图片描述
安装依赖

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
  • 10
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
934909504: found 24 primes in [30000080, 30000279] Thread 123145942302208: found 24 primes in [30000140, 30000339] Thread 123145934909504: found 25 primes in [30000060, 30000259] Thread 123145942302208: found 24 primes in [30000020, 30000219在 CentOS 7 中,yum 和很多系统工具都是基于 Python 2.x 版本开发的,如果你] Thread 123145934909504: found 24 primes in [30000110, 30000309] Thread 123145942卸载了 Python 2.x 版本,那么会导致 yum 无法正常使用。为了解决这个302208: found 25 primes in [30000170, 30000369] Thread 123145934909504: found 24问题,你需要重新安装 Python 2.x 版本或者升级到 Python 3.x 版本。 如果你想重新 primes in [30000050, 30000249] Thread 123145942302208: found 24 primes in [30000120安装 Python 2.x 版本,可以使用以下命令: ``` sudo yum install python2 ``` 如果你想升, 30000319] Thread 123145934909504: found 25 primes in [30000090, 30000289] 级到 Python 3.x 版本,可以使用以下命令: ``` sudo yum install python3 ``` 安装完成后Thread 123145942302208: found 24 primes in [30000030, 30000229] Thread 123145934909,你需要将系统默认的 Python 版本切换到新安装的版本,可以使用以下命令: ``` sudo alternatives504: found 24 primes in [30000100, 30000299] Thread 123145942302208: found 25 primes --config python ``` 然后选择你要使用的 Python 版本即可。 另外,为了避免类似的 in [30000160, 30000359] Thread 123145934909504: found 24 primes in [30000080,问题发生,建议不要轻易卸载系统自带Python 版本,可以通过安装其他版本的 Python 来 30000279] Thread 123145942302208: found 24 primes in [30000140, 30000339] Thread满足自己的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodeDevMaster

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值