1. 程式人生 > >CentOS升級Python27後,yum不可用

CentOS升級Python27後,yum不可用

之前一篇文章介紹了《Centos6.8升級Python2.6為Python2.7》,升級完畢後通過pip安裝依賴時不再提示版本過期,但是隨之而來的問題就是yum不好使了,例如會出現如下報錯

[[email protected] ~]# yum search jpegoptim 
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 (default, May 15 2017, 11:00:16) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)]

If you cannot solve this problem yourself, please go to 
the yum faq at:
  http://yum.baseurl.org/wiki/Faq

問題原因是因為yum不支援python2.7的版本,那麼只需要在/usr/bin/yum中將第一行的#!/usr/bin/python更換為低版本即可。

vim /usr/bin/yum

原來的頭部宣告

#!/usr/bin/python

更換以後的頭部宣告

#!/usr/bin/python2.6

這個python2.6怎麼來的呢?

ll /usr/bin/py*

這裡就會看到系統自帶的以及我們升級後的python

[[email protected] ~]# ll /usr/bin/py*
-rwxr-xr-x 1 root root   78 Aug 18  2016 /usr/bin/pydoc
lrwxrwxrwx 1 root root   34 May 15 11:03 /usr/bin/python -> /usr/local/python2.7/bin/python2.7
lrwxrwxrwx 1 root root    6 Apr 16 23:02 /usr/bin/python2 -> python
-rwxr-xr-x 2 root root 4864 Aug 18  2016 /usr/bin/python2.6
lrwxrwxrwx 1 root root   34 May 15 11:09 /usr/bin/python2.7 -> /usr/local/python2.7/bin/python2.7
-rwxr-xr-x 2 root root 4864 Aug 18  2016 /usr/bin/python_old  

按下esc,並且鍵入:wq即可儲存修改。那麼現在yum包管理器就好使了:

[[email protected] ~]# yum search jpegoptim
Loaded plugins: fastestmirror
Repository epel is listed more than once in the configuration
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * epel: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base                                                                                                                                                                                                             | 3.7 kB     00:00     
epel                                                                                                                                                                                                             | 4.3 kB     00:00     
epel/primary_db                                                                                                                                                                                                  | 5.9 MB     00:05     
extras                                                                                                                                                                                                           | 3.4 kB     00:00     
updates                                                                                                                                                                                                          | 3.4 kB     00:00     
======================================================================================================== N/S Matched: jpegoptim ========================================================================================================
jpegoptim.x86_64 : Utility to optimize JPEG files

  Name and summary matches only, use "search all" for everything.