本人在一顿操作中误删了yum,然后在网上看各种安装方式,一直装不上,还是要根据自己的情况去安装,所以试了各种方式,踩了各种坑最后才装上。以下就是我的安装方式,不能保证所有人都适用,还是要根据自己的情况。
系统是CentOS7.4
重装python2.7和yum
1.查看系统版本:
[root@izwz91xsu9fz728qyw7jodz ~]# lsb_release -a
2.删除现有的python
[root@izwz91xsu9fz728qyw7jodz ~]# rpm -qa|grep python|xargs rpm -ev --allmatches --nodeps ##强制清除已安装的程序及其关联
[root@izwz91xsu9fz728qyw7jodz ~]# whereis python |xargs rm -frv ##删除所有残余文件 ##xargs,允许你对输出执行其他某些命令
[root@izwz91xsu9fz728qyw7jodz ~]# whereis python ##验证删除,返回无结果说明清除干净
3.删除现有的yum
[root@izwz91xsu9fz728qyw7jodz ~]# rpm -qa|grep yum|xargs rpm -ev --allmatches --nodeps
[root@izwz91xsu9fz728qyw7jodz ~]# whereis yum |xargs rm -frv
4.创建一个目录:python-need-pkg,将这些包下载下来,从http://mirrors.163.com/centos/7/os/x86_64/Packages/下载响应的包(注意:根据自己的系统下载,源中版本会更新,具体查看URL中的版本再下载!)
wget http://mirrors.163.com/centos/7/os/x86_64/Packages/libxml2-python-2.9.1-6.el7_2.3.x86_64.rpm
wget http://mirrors.163.com/centos/7/os/x86_64/Packages/lvm2-python-libs-2.02.180-8.el7.x86_64.rpm
wget http://mirrors.163.com/centos/7/os/x86_64/Packages/python-2.7.5-76.el7.x86_64.rpm
wget http://mirrors.163.com/centos/7/os/x86_64/Packages/python-backports-1.0-8.el7.x86_64.rpm
wget http://mirrors.163.com/centos/7/os/x86_64/Packages/python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarch.rpm
wget http://mirrors.163.com/centos/7/os/x86_64/Packages/python-chardet-2.2.1-1.el7_1.noarch.rpm
wget http://mirrors.163.com/centos/7/os/x86_64/Packages/python-devel-2.7.5-76.el7.x86_64.rpm
wget http://mirrors.163.com/centos/7/os/x86_64/Packages/python-iniparse-0.4-9.el7.noarch.rpm
wget http://mirrors.163.com/centos/7/os/x86_64/Packages/python-ipaddress-1.0.16-2.el7.noarch.rpm
wget http://mirrors.163.com/centos/7/os/x86_64/Packages/python-kitchen-1.1.1-5.el7.noarch.rpm
wget http://mirrors.163.com/centos/7/os/x86_64/Packages/python-libs-2.7.5-76.el7.x86_64.rpm
wget http://mirrors.163.com/centos/7/os/x86_64/Packages/python-pycurl-7.19.0-19.el7.x86_64.rpm
wget http://mirrors.163.com/centos/7/os/x86_64/Packages/python-setuptools-0.9.8-7.el7.noarch.rpm
wget http://mirrors.163.com/centos/7/os/x86_64/Packages/python-urlgrabber-3.10-9.el7.noarch.rpm
wget http://mirrors.163.com/centos/7/os/x86_64/Packages/python-virtualenv-15.1.0-2.el7.noarch.rpm
wget http://mirrors.163.com/centos/7/os/x86_64/Packages/rpm-4.11.3-35.el7.x86_64.rpm
wget http://mirrors.163.com/centos/7/os/x86_64/Packages/rpm-build-4.11.3-35.el7.x86_64.rpm
wget http://mirrors.163.com/centos/7/os/x86_64/Packages/rpm-build-libs-4.11.3-35.el7.x86_64.rpm
wget http://mirrors.163.com/centos/7/os/x86_64/Packages/rpm-libs-4.11.3-35.el7.x86_64.rpm
wget http://mirrors.163.com/centos/7/os/x86_64/Packages/rpm-python-4.11.3-35.el7.x86_64.rpm
wget http://mirrors.163.com/centos/7/os/x86_64/Packages/rpm-sign-4.11.3-35.el7.x86_64.rpm
wget http://mirrors.163.com/centos/7/os/x86_64/Packages/yum-3.4.3-161.el7.centos.noarch.rpm
wget http://mirrors.163.com/centos/7/os/x86_64/Packages/yum-metadata-parser-1.1.4-10.el7.x86_64.rpm
wget http://mirrors.163.com/centos/7/os/x86_64/Packages/yum-plugin-aliases-1.1.31-50.el7.noarch.rpm
wget http://mirrors.163.com/centos/7/os/x86_64/Packages/yum-plugin-fastestmirror-1.1.31-50.el7.noarch.rpm
wget http://mirrors.163.com/centos/7/os/x86_64/Packages/yum-plugin-protectbase-1.1.31-50.el7.noarch.rpm
wget http://mirrors.163.com/centos/7/os/x86_64/Packages/yum-utils-1.1.31-50.el7.noarch.rpm
5.执行命令安装
1>执行这一步的时候没有依赖问题
[root@izwz91xsu9fz728qyw7jodz ~]# rpm -Uvh --replacepkgs python*.rpm
2>执行这一步的时候报依赖问题,有两个,
[root@izwz91xsu9fz728qyw7jodz ~]# rpm -Uvh --replacepkgs rpm-python*.rpm yum*.rpm
出现依赖错误
rpm = 4.11.3-35.el7 is needed by rpm-python-4.11.3-35.el7.x86_64
libxml2-python is needed by yum-utils-1.1.31-50.el7.noarch
网上说强制安装,我自己强制安装后,查看yum [root@izwz91xsu9fz728qyw7jodz ~]# yum 报了这样一个错
There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:
/usr/lib64/python2.7/site-packages/rpm/_rpm.so: undefined symbol: rpmtsAddReinstallElement
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.5 (default, Oct 30 2018, 23:45:53)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
If you cannot solve this problem yourself, please go to
the yum faq at:
http://yum.baseurl.org/wiki/Faq
可能代表着你的rpm包没安装全
在我安装[root@izwz91xsu9fz728qyw7jodz python-need-pkgs]# rpm -ivh rpm-python-4.11.3-35.el7.x86_64.rpm 一直重复报这个依赖错误
我在https://segmentfault.com/q/1010000009194060这个评论看到解决方法
当我使用下列命令查看时,发现版本是25的
[root@izwz91xsu9fz728qyw7jodz python-need-pkgs]# rpm -qa|grep rpm
rpm-build-libs-4.11.3-25.el7.x86_64
rpm-4.11.3-25.el7.x86_64
perl-srpm-macros-1-8.el7.noarch
rpm-libs-4.11.3-25.el7.x86_64
rpm-python-4.11.3-25.el7.x86_64
redhat-rpm-config-9.1.0-87.el7.centos.noarch
与我要安装的rpm-python-4.11.3-35.el7.x86_64.rpm的rpm版本不一致,我采用了如下办法
我更新了rpm的三个包来使我安装的版本和rpm版本一致:
[root@izwz91xsu9fz728qyw7jodz python-need-pkgs]# rpm -Uvh rpm-4.11.3-35.el7.x86_64.rpm rpm-build-libs-4.11.3-35.el7.x86_64.rpm rpm-libs-4.11.3-35.el7.x86_64.rpm
这时候再去安装[root@izwz91xsu9fz728qyw7jodz python-need-pkgs]# rpm -ivh rpm-python-4.11.3-35.el7.x86_64.rpm 就可以了
还有一个依赖解决 libxml2-python is needed by yum-utils-1.1.31-50.el7.noarch 我选择的是强制安装
[root@izwz91xsu9fz728qyw7jodz python-need-pkgs]# rpm -ivh yum-utils-1.1.31-50.el7.noarch.rpm --force --nodeps
最后安装:[root@izwz91xsu9fz728qyw7jodz ~]# rpm -Uvh --replacepkgs rpm-python*.rpm yum*.rpm 就安装成功了
6.查看python版本,命令正常
[root@izwz91xsu9fz728qyw7jodz python-need-pkgs]# python
Python 2.7.5 (default, Jun 20 2019, 20:27:34)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
7.查看yum,命令正常
[root@izwz91xsu9fz728qyw7jodz python-need-pkgs]# yum
Loaded plugins: aliases, fastestmirror, protectbase
You need to give some command
Usage: yum [options] COMMAND
8.在yum安装成功后,可将源文件清空并下载新的源
可参考:https://blog.csdn.net/qq_36731677/article/details/58288979
之后逐步更新源数据
[root@izwz91xsu9fz728qyw7jodz python-need-pkgs]# yum clean all
[root@izwz91xsu9fz728qyw7jodz python-need-pkgs]# yum makecache
[root@izwz91xsu9fz728qyw7jodz python-need-pkgs]# yum --skip-broken update
我是linux菜鸟,很多也不懂,这是我成功安装的方法,希望能帮助到一些人,不懂得各位大神请多多指教!