centos7.9.2009重装python和yum

准备在服务上部署一个项目,但是这个项目要求python版本必须大于3.6,而centos默认是2.7的,于是去网上搜索升级文章。然后照着文章,执行了这几条命令

rm -f /usr/bin/python
ln -s /usr/local/bin/python3 /usr/bin/python
rm /usr/bin/pip
ln -s /usr/local/bin/pip

然后我的centos系统就直接无法使用yum和python命令了,坑死我了。

整个过程可以理解为误删除python,导致yum不可用,所以现在要做的就是重装python和yum。

1、删除现有Python和yum

要重装的话,就需要删干净python和yum,可以执行如下命令

# 删除python
rpm -qa|grep python|xargs rpm -ev --allmatches --nodeps
whereis python |xargs rm -frv
whereis python 

# 删除yum
rpm -qa|grep yum|xargs rpm -ev --allmatches --nodeps 
whereis yum |xargs rm -frv

2、查看Linux系统版本

这一步是一定要执行的,因为后续需要下载相关依赖的,根据版本来下载

[root@VM-4-5-centos ~]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)

我的 centos 版本为7.9.2009

3、下载依赖

这里我选用的镜像站点为 阿里云开源镜像站 对应7.9.2009的地址为 https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/ 这里根据你的版本替换。

创建存放yum和python相关依赖包的目录

mkdir python_and_yum && cd $_

然后执行如下命令安装依赖

wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/lvm2-python-libs-2.02.187-6.el7.x86_64.rpm
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/libxml2-python-2.9.1-6.el7.5.x86_64.rpm
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages//python-libs-2.7.5-89.el7.x86_64.rpm
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/python-ipaddress-1.0.16-2.el7.noarch.rpm
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/python-backports-1.0-8.el7.x86_64.rpm
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarch.rpm
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/python-2.7.5-89.el7.x86_64.rpm 
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/python-iniparse-0.4-9.el7.noarch.rpm 
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/python-pycurl-7.19.0-19.el7.x86_64.rpm 
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/python-urlgrabber-3.10-10.el7.noarch.rpm 
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/python-setuptools-0.9.8-7.el7.noarch.rpm
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/python-kitchen-1.1.1-5.el7.noarch.rpm 
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/python-chardet-2.2.1-3.el7.noarch.rpm
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/rpm-python-4.11.3-45.el7.x86_64.rpm
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/yum-utils-1.1.31-54.el7_8.noarch.rpm 
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/yum-3.4.3-168.el7.centos.noarch.rpm 
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/yum-metadata-parser-1.1.4-10.el7.x86_64.rpm 
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/yum-plugin-aliases-1.1.31-54.el7_8.noarch.rpm 
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/yum-plugin-protectbase-1.1.31-54.el7_8.noarch.rpm 
wget https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/yum-plugin-fastestmirror-1.1.31-54.el7_8.noarch.rpm

要注意,不同版本的依赖文件不同,这个就需要到官网上找对应的依赖文件替换(可能需要手动替换,否则安装会提示404 Not Found),以上演示的都为7.9.2009版本的依赖

4、安装依赖

rpm -Uvh --replacepkgs lvm2-python-libs*.rpm --nodeps --force
rpm -Uvh --replacepkgs libxml2-python*.rpm --nodeps --force
rpm -Uvh --replacepkgs python*.rpm --nodeps --force
rpm -Uvh --replacepkgs rpm-python*.rpm yum*.rpm --nodeps --force

如果不加 --nodeps --force 可能会提示依赖找不到,但实际上已经安装了。

5、大功告成

执行完毕后,再次在终端中就能查看对应版本信息。

image-20220903193447856

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