天天看點

CentOS 更新 Python2.4.3 到 2.7.5

檢視 python 的版本

[[email protected] ~]# python -V
Python 2.4.3
           

下載下傳并安裝 Python-2.7.5

[[email protected] ~]# wget http://python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2
[[email protected] ~]# tar -jxvf Python-2.7.5.tar.bz2
[[email protected] ~]# cd Python-2.7.5
[[email protected] Python-2.7.5]# ./configure
[[email protected] Python-2.7.5]# make && make install
           

建立軟連接配接,使系統預設的 python 指向 python2.7

      正常情況下,即使 python2.7 安裝成功後,系統預設指向的 python 仍然是 2.4.3 版本,考慮到 CentOS5.6 系統中的 yum 是基于 python2.4 才能正常工作,是以不要解除安裝 python2.4 版本。 那麼 如何 實作将系統預設的 python 指向到 2.7 版本呢?

未做修改前:

[[email protected] Python-2.7.5]# ll /usr/bin/python*
-rwxr-xr-x 2 root root 8304 Mar  6  2011 /usr/bin/python
lrwxrwxrwx 1 root root    6 Jul  4  2013 /usr/bin/python2 -> python
-rwxr-xr-x 2 root root 8304 Mar  6  2011 /usr/bin/python2.4
           

做如下修改:

[[email protected] Python-2.7.5]# rm -f /usr/bin/python2
[[email protected] Python-2.7.5]# mv /usr/bin/python /usr/bin/python2.4
[[email protected] Python-2.7.5]# ln -s /usr/local/bin/python2.7 /usr/bin/python
           

上面的 3 步分别為:

1.删除之前的軟連接配接;

2.将預設 python 重命名為 python2.4 以給 yum 使用(這裡可以省略該步驟,因為 python2.4 本身就存在);

3.将預設 python 軟連接配接到 python2.7 上(這裡要看新裝的 python2.7 是否已存在到 python 的軟連接配接)。

檢驗 python 指向是否成功

[[email protected] Python-2.7.5]# python -V
Python 2.7.5
           

解決預設 python 軟連結指向 python2.7 版本後 yum 不能正常工作的問題

[[email protected] Python-2.7.5]# vi /usr/bin/yum

#!/usr/bin/python
...
           

将檔案頭部的

#!/usr/bin/python
           

改成

#!/usr/bin/python2.4
           

整個更新過程完成,可以使用 Python2.7.5 版本了。

============== 我是分割線  =============

當預設 python 更新到 2.7.5 後,在未修改 /usr/bin/yum 時,運作 yum 相關指令會得到如下錯誤:

[[email protected] Python-2.7.5]# yum list
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.5 (default, Jul  5 2013, 02:21:36) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)]

If you cannot solve this problem yourself, please go to 
the yum faq at:
  http://wiki.linux.duke.edu/YumFaq
 [[email protected] Python-2.7.5]# 
           

這是因為 yum 對 python 版本具有依賴性的原因。/usr/bin/yum 的内容如下:

[[email protected] Python-2.7.5]# vi /usr/bin/yum 

#!/usr/bin/python
import sys
try:
    import yum
except ImportError:
    print >> sys.stderr, """\
There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:

   %s

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:
%s

If you cannot solve this problem yourself, please go to
the yum faq at:
  http://wiki.linux.duke.edu/YumFaq

""" % (sys.exc_value, sys.version)
    sys.exit(1)

sys.path.insert(0, '/usr/share/yum-cli')
try:
    import yummain
    yummain.user_main(sys.argv[1:], exit_code=True)
except KeyboardInterrupt, e:
    print >> sys.stderr, "\n\nExiting on user cancel."
    sys.exit(1)
~