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