【整理】CentOS5.6 升级 Python2.4.3 到 2.7.5

本文介绍如何在CentOS系统中升级Python版本至2.7.5,并解决升级后带来的默认Python版本指向及yum命令无法正常工作等问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



查看 python 的版本  
?
1
2
[root@localhost ~] # python -V
Python 2.4.3

下载并安装 Python-2.7.5  
?
1
2
3
4
5
6
[root@localhost ~] # wget http://python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2
[root@localhost ~] # tar -jxvf Python-2.7.5.tar.bz2
[root@localhost ~] # cd Python-2.7.5
[root@localhost Python-2.7.5] # ./configure
[root@localhost Python-2.7.5] # make
[root@localhost Python-2.7.5] # make install

建立软连接,使系统默认的 python 指向 python2.7

      正常情况下,即使 python2.7 安装成功后,系统默认指向的 python 仍然是 2.4.3 版本,考虑到 CentOS5.6 系统中的 yum 是基于 python2.4 才能正常工作,所以不要卸载 python2.4 版本。   那么   如何   实现将系统默认的 python 指向到 2.7 版本呢?  

未做修改前:  
?
1
2
3
4
[root@localhost 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
做如下修改:  
?
1
2
3
[root@localhost Python-2.7.5] # rm -f /usr/bin/python2
[root@localhost Python-2.7.5] # mv /usr/bin/python /usr/bin/python2.4
[root@localhost 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 指向是否成功  
?
1
2
[root@localhost Python-2.7.5] # python -V
Python 2.7.5

解决默认 python 软链接指向 python2.7 版本后 yum 不能正常工作的问题  
?
1
2
3
4
[root@localhost Python-2.7.5] # vi /usr/bin/yum
 
#!/usr/bin/python
...
将文件头部的  
?
1
#!/usr/bin/python
改成  
?
1
#!/usr/bin/python2.4
整个升级过程完成,可以使用 Python2.7.5 版本了。  


============== 我是分割线     =============  

当默认 python 升级到 2.7.5 后,在未修改 /usr/bin/yum 时,运行 yum 相关命令会得到如下错误:  
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@localhost 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
   
 
[root@localhost Python-2.7.5] #
这是因为 yum 对 python 版本具有依赖性的原因。/usr/bin/yum 的内容如下:  
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[root@localhost 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)
~


============== 我是分割线    2015-09-08  =============  

上述变更方式,改动比较大,另外一种方式为,保留系统中已有 Python 相关内容不变,仅通过变更环境变量的方式引用新版本 Python 即可。

PS: 若出现错误 " ImportError: No module named bz2 " ,则需要先安装  bzip2-devel  ,再重新编译 Python 即可(正统方法);或者,执行
?
1
cp /usr/lib64/python2 .6 /lib-dynload/bz2 .so /usr/local/lib/python2 .7/
也可以(野路子)。Stackoverflow 上的讨论,看这里
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值