首頁 > 軟體

Python升級導致yum不可用 「 No module named yum」

2020-06-16 17:09:09

最近在看Python,虛擬機器裝的是CentOS 6.6,自帶的Python版本是2.6.6,打算升級到2.7。

我的升級過程大致如下:

下載2.7原始碼包https://www.python.org/downloads/source/

解除安裝舊的python,rpm -e python

編譯安裝python2.7

整個過程沒有遇到問題,但升級完後,再用yum安裝軟體時報如下錯誤

[root@localhost python]# 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.7.13 (r266:84292, Jan 22 2014, 09:37:14) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)]
 
If you cannot solve this problem yourself, please go to 
the yum faq at:
  http://yum.baseurl.org/wiki/Faq

yum不可用了,這下問題就大了,spacer.gif沒有yum,安裝rpm包特別費勁。
開始百度關鍵字“No module named yum”,出來很多貼文和部落格,部落格上邊說的大概意思就是yum就基於python的,升級python後,yum與高版本的python不相容,導致yum無法使用。部落格上給出的方法都是修改/usr/bin/yum檔案的頭部,把/usr/bin/python修改為/usr/bin/python2.6就可以了,但我的情況是我把舊的python已經解除安裝了,於是又開始安裝2.6版本的python。
安裝好2.6.6版本的之後以為就好了,但是還是報有錯誤:
[root@localhost python]# 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, Jan 22 2014, 09:37:14) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)]
 
If you cannot solve this problem yourself, please go to 
the yum faq at:
  http://yum.baseurl.org/wiki/Faq

這就奇怪了,都已經把舊版本的裝上了,還是不行。這時看到一篇部落格是把python和yum都全部解除安裝後重新安裝的。想想這也是個辦法於是就又全部下載
whereis python |xargs rm -rf
rpm -e --nodeps python

解除安裝後重新安裝,從光碟映象裡找到python和yum的包
rpm -ivh --nodeps python*
rpm -ivh --nodeps yum*

這次總該好了吧,驗證一下
[root@localhost python]# python
Python 2.6.6 (r266:84292, Jan 22 2014, 09:37:14) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import yum
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/site-packages/yum/__init__.py", line 23, in <module>
    import rpm
ImportError: No module named rpm

還是報錯。不過這次錯誤變了,變成了“No module named rpm”
再次百度,看到有人說是缺少包:rpm -ivh rpm-python-4.8.0-37.el6.i686.rpm
安裝上面的包後一切yum恢復正常了。


那問題又來了難道因為yum就不升級python版本嗎?當然不是。其實是我在安裝新版python的時候把舊版本的也解除安裝了才導致這樣的問題。

後來在不解除安裝2.6版本的python是,重新編譯安裝2.7版本,安裝成功並且yum仍然可用。

本文永久更新連結地址http://www.linuxidc.com/Linux/2017-08/146518.htm


IT145.com E-mail:sddin#qq.com