centos5.x升级至python2.7、mysql-python

centos5.3的python版本真让人泪奔,什么时代了还2.4的。自己动手丰衣足食。

1.从官网下载2.7版本

 

# wget http://www.python.org/ftp/python/2.7.1/Python-2.7.1.tar.bz2 

 

2.安装

 

# tar -jxvf Python-2.7.1.tar.bz2  
# cd Python-2.7.1 
# ./configure   (如不指定 --prefix,默认安装在/usr/local/lib/python2.7)
# make && make install

 

3.升级默认版本

 

# mv python /usr/bin/python24
# ln -s /usr/local/bin/python2.7 /usr/bin/python 

 

4.安装mysql-python

(安装方法不了解可以参看包内的README)

# python setup.py build
Traceback (most recent call last):
  File "setup.py", line 5, in 
<module>
    from setuptools import setup, Extension
ImportError: No module named setuptools 

什么情况?貌似setuptools没有

5.去官网下载setuptools

官网下载地址

 # bash setuptools-0.6c11-py2.7.egg

Traceback (most recent call last):

  File "<string>", line 1, in <module>

zipimport.ZipImportError: can't decompress data; zlib not available

 又是什么情况?缺少zlib?(关于安装python所需的软件环境可参看README)

这一步我花了很长时间解决它,过程实在是很让人不爽。从上面这个错误提示似乎是zlib没有,最先想到的是使用yum安装zlib*。

# rpm -qa | grep zlib
zlib-1.2.3-3
zlib-devel-1.2.3-3

 重新编译python以后,依旧提示这个错误"zipimport.ZipImportError: can't decompress data; zlib not available",到最后都有点抓狂了。快要发飙的时候浏览了zlib的官站,它的最新版本是zlib-1.2.5。我决定用 tar包安装最新版本试试。

安装过程非常简单,仍是老三步:

# cd zlib-1.2.5
# ./configure

# make && make install  


在解决该问题的过程中,我发现了python安装方法中最简易的一种“easy_install ,将使用它来安装mysql-python

# wget http://peak.telecommunity.com/dist/ez_setup.py
# python ez_setup.py 
Downloading http://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg
Processing setuptools-0.6c11-py2.7.egg
Copying setuptools-0.6c11-py2.7.egg to /usr/local/lib/python2.7/site-packages
Adding setuptools 0.6c11 to easy-install.pth file
Installing easy_install script to /usr/local/bin
Installing easy_install-2.7 script to /usr/local/bin

Installed /usr/local/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg
Processing dependencies for setuptools==0.6c11

Finished processing dependencies for setuptools==0.6c11  


在上一步步骤中,它自行安装了setuptools-0.6c11-py2.7.egg,这样就可以使用 easy_install 了。

4.安装MySQL-python

# easy_install MySQL-python
Searching for MySQL-python
Reading http://pypi.python.org/simple/MySQL-python/
Reading http://sourceforge.net/projects/mysql-python/
Reading http://sourceforge.net/projects/mysql-python
Best match: MySQL-python 1.2.3
Downloading http://download.sourceforge.net/sourceforge/mysql-python/MySQL-python-1.2.3.tar.gz
Processing MySQL-python-1.2.3.tar.gz
Running MySQL-python-1.2.3/setup.py -q bdist_egg --dist-dir /tmp/easy_install-0IG0OI/MySQL-python-1.2.3/egg-dist-tmp-pBvQ9I
sh: mysql_config: command not found
Traceback (most recent call last):
  File "/usr/local/bin/easy_install", line 8, in 
<module>
    load_entry_point('setuptools==0.6c11', 'console_scripts', 'easy_install')()
  File "/usr/local/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/setuptools/command/easy_install.py", line 1712, in main
  File "/usr/local/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/setuptools/command/easy_install.py", line 1700, in with_ei_usage
  File "/usr/local/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/setuptools/command/easy_install.py", line 1716, in 
<lambda>
  File "/usr/local/lib/python2.7/distutils/core.py", line 152, in setup
    dist.run_commands()
  File "/usr/local/lib/python2.7/distutils/dist.py", line 953, in run_commands
    self.run_command(cmd)
  File "/usr/local/lib/python2.7/distutils/dist.py", line 972, in run_command
    cmd_obj.run()
  File "/usr/local/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/setuptools/command/easy_install.py", line 211, in run
  File "/usr/local/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/setuptools/command/easy_install.py", line 446, in easy_install
  File "/usr/local/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/setuptools/command/easy_install.py", line 476, in install_item
  File "/usr/local/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/setuptools/command/easy_install.py", line 655, in install_eggs
  File "/usr/local/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/setuptools/command/easy_install.py", line 930, in build_and_install
  File "/usr/local/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/setuptools/command/easy_install.py", line 919, in run_setup
  File "/usr/local/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/setuptools/sandbox.py", line 62, in run_setup
  File "/usr/local/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/setuptools/sandbox.py", line 105, in run
  File "/usr/local/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/setuptools/sandbox.py", line 64, in 
<lambda>
  File "setup.py", line 15, in 
<module>
  File "/tmp/easy_install-0IG0OI/MySQL-python-1.2.3/setup_posix.py", line 43, in get_config
  File "/tmp/easy_install-0IG0OI/MySQL-python-1.2.3/setup_posix.py", line 24, in mysql_config

EnvironmentError: mysql_config not found  


以上错误提示没有安装mysql环境,需要安装mysql,此处省略N个字,安装好mysql以后,再度安装MySQL-python

# easy_install MySQL-python 

Searching for MySQL-python
Reading http://pypi.python.org/simple/MySQL-python/
Reading http://sourceforge.net/projects/mysql-python/
Reading http://sourceforge.net/projects/mysql-python
Best match: MySQL-python 1.2.3
Downloading http://download.sourceforge.net/sourceforge/mysql-python/MySQL-python-1.2.3.tar.gz
Processing MySQL-python-1.2.3.tar.gz
Running MySQL-python-1.2.3/setup.py -q bdist_egg --dist-dir /tmp/easy_install-w7wAW2/MySQL-python-1.2.3/egg-dist-tmp-CCgmn2
warning: no files found matching 'MANIFEST'
warning: no files found matching 'ChangeLog'
warning: no files found matching 'GPL'
......
zip_safe flag not set; analyzing archive contents...
Adding MySQL-python 1.2.3 to easy-install.pth file

Installed /usr/local/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-linux-x86_64.egg
Processing dependencies for MySQL-python

Finished processing dependencies for MySQL-python

 

5.此时应该算是将mysql-python安装上了,检验一下: 

 # python

Python 2.7.1 (r271:86832, Apr 27 2011, 15:54:05) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
Traceback (most recent call last):  File "build/bdist.linux-x86_64/egg/_mysql.py", line 6, in __bootstrap__

  File "<stdin>", line 1, in <module>  File "build/bdist.linux-x86_64/egg/MySQLdb/__init__.py", line 19, in <module>  File "build/bdist.linux-x86_64/egg/_mysql.py", line 7, in <module>ImportError: libmysqlclient.so.16: cannot open shared object file: No such file or directory

 

果然是让人神伤啊,99步都走过了,不差这临门一脚。编辑ld.so.conf文件,添加libmysqlclient.so.16所在目录:

 # vi /etc/ld.so.conf 

/usr/local/innosql-5.5.8-linux-x86_64/lib/  

# ldconfig -v 

 

再次检验,KO

# python
Python 2.7.1 (r271:86832, Apr 27 2011, 15:54:05) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb

>>> 

 

上面的安装过程虽然没什么技术含量,但非常烦人。这里真得希望redhat系的大叔们能稍微把自带的python版本升高一点吧!

 

参考:

http://www.question-defense.com/2009/03/16/install-easy_install-via-yum-on-linux-centos-server 

http://villaroad.com/2010/10/rolling-python-2-6-2-on-centos-5-3/ 

http://toey.tc20.net/2010/08/04/install-python-2-7-gevent-on-centos-5-x86_64/ 

 http://www.sou5.cn/archives/tag/easy_install

http://my.oschina.net/captaintheron/blog/3668

http://blog.csdn.net/wxkingstar/archive/2009/09/08/4530651.aspx 

posted @ 2011-04-20 16:50 番茄侠 阅读(...) 评论(...) 编辑 收藏