CentOS 5升级Python版本(2.4>2.7)

日前在CentOS上搭建测试环境时候,遇到需要升级python版本的情况,于是就记录了整个升级的过程:

在CentOS5中自带的Python版本是2.4,但是目前许多基于Python的应用软件要求的Python版本应要高于2.4。升级 python版本的时候千万不能卸载python 2.4,再安装python2.7,这样会有无穷无尽的麻烦,保守的方式是直接安装python2.7的源码包,也就是python两个版本共存。(因为 Centos里面有很多程序是依赖着python,所有最好不要尝试去卸载python2.4)。

(1)下载/安装python

下载Python2.7.2.tgz(# wget http://www.python.org/ftp/python/2.7.2/Python-2.7.2.tgz)

  1. $tar jxvf Python2.7.2.tgz
  2. $cd Python2.7.2
  3. $./configure
  4. $make && make install

自此,python2.7安装后路径默认是在/usr/local/lib/python2.7
查看Python版本:
$ /usr/local/bin/python2.7 -V
(2)建立软连接,使系统默认的python指向python2.7
正常情况下即使python2.7安装成功后,系统默认指向的python仍然是2.4版本,考虑到yum是基于
python2.4才能正常工作,不敢轻易卸载。如何实现将系统默认的python指向到2.7版本呢?
mv /usr/bin/python /usr/bin/python.bak (或者rm -rf /usr/bin/python)
ln -s /usr/local/bin/python2.7 /usr/bin/python
检验python指向是否成功
python -V
(3) 解决系统python软链接指向python2.7版本后,yum不能正常工作
方法:
$vi /usr/bin/yum
将文本编辑显示的第一行
#!/usr/bin/python修改为#!/usr/bin/python2.4,保存修改即可

-----------------------------------------------------

安装前确认zlib是否安装?没安装 请yum install zlib zlib-devel
wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz
tar zxvf Python-2.7.3.tgz
cd Python-2.7.3
./configure --prefix=/usr && make && make install
(也可以考虑不覆盖老版本,自行定义安装路径,最后做软链接)
安装完毕,但会发现CentOS yum需要系统自带的2.4版本,主要一定修改yum,否则会导致yum不能使用。
vi /usr/bin/yum
第1行
#!/usr/bin/python修改为:#!/usr/bin/python2.4

然后安装EasyInstall
wget http://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-p2y2.7.egg
sh setuptools-0.6c11-p2y2.7.egg
安装完成后就可以直接使用easy_install命令安装第三方Python开发包了,例如:easy_install SQLObject
全部自动完成,不用一个个去找egg了。

-----------------------------------------------------

找到问题了原因了,问题已经解决,解决方法如下:
将原来好用的机器上的/usr/lib/python2.4整个目录拷贝到本机的相应路径下,同时,拷贝好用机器上的/usr/bin/python*到本机相应路径下。
#scp -r 192.168.1.2:/usr/lib/python2.4  /usr/lib/
#scp -r 192.168.1.2:/usr/bin/python*   /usr/bin/
# vi /etc/profile
添加环境变量
export PATH="/usr/bin/:$PATH"
#source /etc/profile
# python -v  重新导入
出现如下字样,即可正常:
# installing zipimport hook
import zipimport # builtin
# installed zipimport hook
# /usr/lib/python2.4/site.pyc matches /usr/lib/python2.4/site.py
import site # precompiled from /usr/lib/python2.4/site.pyc
# /usr/lib/python2.4/os.pyc matches /usr/lib/python2.4/os.py
import os # precompiled from /usr/lib/python2.4/os.pyc
import posix # builtin
# /usr/lib/python2.4/posixpath.pyc matches /usr/lib/python2.4/posixpath.py
import posixpath # precompiled from /usr/lib/python2.4/posixpath.pyc
# /usr/lib/python2.4/stat.pyc matches /usr/lib/python2.4/stat.py
import stat # precompiled from /usr/lib/python2.4/stat.pyc
import errno # builtin
# /usr/lib/python2.4/UserDict.pyc matches /usr/lib/python2.4/UserDict.py
import UserDict # precompiled from /usr/lib/python2.4/UserDict.pyc
# /usr/lib/python2.4/copy_reg.pyc matches /usr/lib/python2.4/copy_reg.py
import copy_reg # precompiled from /usr/lib/python2.4/copy_reg.pyc
# /usr/lib/python2.4/types.pyc matches /usr/lib/python2.4/types.py
import types # precompiled from /usr/lib/python2.4/types.pyc
import japanese # directory /usr/lib/python2.4/site-packages/japanese
# /usr/lib/python2.4/site-packages/japanese/__init__.pyc matches /usr/lib/python2.4/site-packages/japanese/__init__.py
import japanese # precompiled from /usr/lib/python2.4/site-packages/japanese/__init__.pyc
import japanese.aliases # directory /usr/lib/python2.4/site-packages/japanese/aliases
# /usr/lib/python2.4/site-packages/japanese/aliases/__init__.pyc matches /usr/lib/python2.4/site-packages/japanese/aliases/__init__.py
import japanese.aliases # precompiled from /usr/lib/python2.4/site-packages/japanese/aliases/__init__.pyc
import encodings # directory /usr/lib/python2.4/encodings
# /usr/lib/python2.4/encodings/__init__.pyc matches /usr/lib/python2.4/encodings/__init__.py
import encodings # precompiled from /usr/lib/python2.4/encodings/__init__.pyc
# /usr/lib/python2.4/codecs.pyc matches /usr/lib/python2.4/codecs.py
import codecs # precompiled from /usr/lib/python2.4/codecs.pyc
import _codecs # builtin
# /usr/lib/python2.4/encodings/aliases.pyc matches /usr/lib/python2.4/encodings/aliases.py
import encodings.aliases # precompiled from /usr/lib/python2.4/encodings/aliases.pyc
# /usr/lib/python2.4/warnings.pyc matches /usr/lib/python2.4/warnings.py
import warnings # precompiled from /usr/lib/python2.4/warnings.pyc
# /usr/lib/python2.4/linecache.pyc matches /usr/lib/python2.4/linecache.py
import linecache # precompiled from /usr/lib/python2.4/linecache.pyc
# /usr/lib/python2.4/encodings/utf_8.pyc matches /usr/lib/python2.4/encodings/utf_8.py
import encodings.utf_8 # precompiled from /usr/lib/python2.4/encodings/utf_8.pyc
Python 2.4.3 (#1, Sep  3 2009, 15:37:12)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-46)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
dlopen("/usr/lib/python2.4/lib-dynload/readline.so", 2);
import readline # dynamically loaded from /usr/lib/python2.4/lib-dynload/readline.so
>>>
# python
Python 2.4.3 (#1, Sep  3 2009, 15:37:12)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-46)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
现在,终于找回久违的python2.4.3了,再执行yum安装一个包试试。
# yum install rrdtool
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* addons: centos.tt.co.kr
* base: ftp.cse.yzu.edu.tw
* extras: centos.tt.co.kr
* updates: data.nicehosting.co.kr
addons                                                                                                       |  951 B     00:00
一切ok了。

-----------------------------------------------------

http://bbs.chinaunix.net/thread-1815457-1-1.html

http://www.linuxidc.com/Linux/2012-02/55194.htm

http://www.linuxidc.com/Linux/2012-05/61045.htm

posted @ 2012-10-23 09:42  adodo1  Views(136)  Comments(0Edit  收藏  举报