忙碌了兩個小時的陳師傅,決定發一篇博客以防其他網友繼續撞坑!!!
問題大致如下:
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:
錯誤原因:你現在安裝的python跟yum所依賴的python不相符,請安裝相對應的python。
大部分人出現這個錯誤的原因在於拿到服務器的第一時間發現python版本是2.x,想升級到python3 導致yum損壞,博主花了兩個小時查閱資料,最終總算解決問題。
方案一:更換軟鏈接
- 查看已安裝python版本,可能是當前系統存在多個python導致
root@local:[/root] whereis python //尋找python所有路徑
python: /usr/bin/python /usr/bin/python2.7 /usr/bin/python.bak /usr/lib/python2.7 /usr/lib64/python2.7 /etc/python /usr/include/python2.7 /usr/share/man/man1/python.1.gz
- 更換python軟鏈接
root@local:[/usr/bin] mv python python.bak //備份的好習慣一定不能忘
root@local:[/usr/bin] ln -s /usr/bin/python2.7 /usr/bin/python //將python2.7軟鏈接
- 查看python版本
root@local:[/usr/bin] python -V
Python 2.7.5 //還原python默認版本
方案二:更換yum頭(此方法同一,好處是你將python3設置軟鏈接之後yum也可以用)
- 同方案一第一步查看python版本之後,找到/usr/bin/python2.x,直接執行python2.x
root@local:[/usr/bin] python2.7
Python 2.7.5 (default, Apr 2 2020, 13:16:51)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
- 嘗試引入yum庫
如果沒有任何反應,那麼此版本即爲yum所依賴的版本,如果出現錯誤,那麼只需要另換2.x繼續重複此操作,最終找到對應版本即可(如果此方法無效,直接看方案三)。
- 編輯yum.py文件
找到yum文件,編輯此文件
root@local:[/root] which yum
/usr/bin/yum
root@local:[/root] vi /usr/bin/yum
將第一行的#!/usr/bin/python 改成 #!/usr/bin/python2.x
在某度查了幾篇文章發現都是這個方法,你們可以試試,我想應該沒什麼用。
方案三:完全刪除python及yum重新安裝
- 刪除現有的python(這裏借用51CTO博客的@SmilePad博主的代碼)
[root@test ~]# rpm -qa|grep python|xargs rpm -ev --allmatches --nodeps ##強制刪除已安裝程序及其關聯
[root@test ~]# whereis python |xargs rm -frv ##刪除所有殘餘文件 ##xargs,允許你對輸出執行其他某些命令
[root@test ~]# whereis python ##驗證刪除,返回無結果
- 刪除現有的yum
[root@test ~]# rpm -qa|grep yum|xargs rpm -ev --allmatches --nodeps
[root@test ~]# whereis yum |xargs rm -frv
-
從中科,163或者阿里雲鏡像下載相對應的包
centos版本可以根據自己的版本去找對應Packages
如果不知道自己的系統版本,命令行輸入這個cat /etc/redhat-release
中科: http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/
163 : http://mirrors.163.com/centos/7/os/x86_64/Packages/
阿里: https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/
Python跟yum的安裝包根據時間的變化版本也會變動,以下版本爲2018.06.12使用,最新版本根據文件的前綴自行下載。
- 下載Python安裝包(此處引用簡書的@挨踢的懶貓大大的原話)
我們需要下載的rpm文件列表:
- python-libs-2.7.5-48.el7.x86_64.rpm,被python依賴
- python-2.7.5-48.el7.x86_64.rpm
- python-iniparse-0.4-9.el7.noarch.rpm, 被yum依賴
- python-pycurl-7.19.0-19.el7.x86_64.rpm, 被python-urlgrabber依賴
- python-urlgrabber-3.10-8.el7.noarch.rpm , 被yum依賴
- rpm-python-4.11.3-21.el7.x86_64.rpm , 被yum依賴
- 作者:挨踢的懶貓
鏈接:https://www.jianshu.com/p/89df82a5d74b
來源:簡書
著作權歸作者所有。商業轉載請聯繫作者獲得授權,非商業轉載請註明出處。
下載大家都會吧,用wget下載到服務器上,下載好了之後一定要按照上面的順序依次安裝。
安裝命令
rpm -ivh xxx.rpm
- 下載yum安裝包
yum安裝包列表:
- yum-3.4.3-150.el7.centos.noarch.rpm, 就是它依賴了上面的python庫
- yum-metadata-parser-1.1.4-10.el7.x86_64.rpm
- yum-plugin-fastestmirror-1.1.31-40.el7.noarch.rpm
- 作者:挨踢的懶貓
鏈接:https://www.jianshu.com/p/89df82a5d74b
來源:簡書
著作權歸作者所有。商業轉載請聯繫作者獲得授權,非商業轉載請註明出處。
yum安裝也按照順序執行,在執行第一個rpm時, 結尾加上 --nodeps參數強制安裝
此處圖片轉載自@有人敲門knocky
最後大功告成!!!
修復yum報錯問題之後,如果還想安裝python3.x 以及 pip3 請看此篇文章:Linux下 python2.x與python3.x共存
root@local:[/usr/bin] yum
***************************
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
makecache Generate the metadata cache
provides Find what package provides the given value
reinstall reinstall a package
repo-pkgs Treat a repo. as a group of packages, so we can install/remove all of them
repolist Display the configured software repositories
search Search package details for the given string
shell Run an interactive yum shell
swap Simple way to swap packages, instead of using shell
update Update a package or packages on your system
update-minimal Works like upgrade, but goes to the 'newest' package match which fixes a problem that affects your system
updateinfo Acts on repository update information
upgrade Update packages taking obsoletes into account
version Display a version for the machine and/or available repos.
點個關注再走唄~