请选择合适的方式,下载python2.7.3安装包
http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tar.bz2
安装前当然需要编译器gcc
- yum install gcc
为了安装setuptools,要需要安装zlib
- yum install zlib zlib-dev
解压安装python2.7
- tar jxvf Python-2.7.3.tar.bz2
- cd Python-2.7.3
- ./configure
- make && make instal
这里请注意:
上面这一步是正常步骤,但是在centos5.7环境中,当你执行setuptools时可能会有以下报错:
- Traceback (most recent call last):
- File "<string>", line 1, in <module>
- zipimport.ZipImportError: can't decompress data; zlib not available
解决方法是在上面的configure之后,编辑Modules/Setup文件
找到下面这句,去掉注释
- #zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz
重新编译安装:
- make & make install
把系统自带的2.4移除(依然会保留2.4版本:/usr/bin/python2.4)
- rm -f /usr/bin/python
把python执行软连接连接到2.7
- ln -s /usr/local/bin/python2.7 /usr/bin/python
编辑yum命令,把路径指明为2.4,因为yum必须基于2.4版本
- sed -ie 's#/usr/bin/python$#/usr/bin/python2.4#g' /usr/bin/yum
对应python2.7.3版本的是
http://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg
下载后,执行
- sh 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
安装mysql相关包
- yum install MySQL-devel MySQL-client MySQL-shared-compat MySQL-shared
下载好mysqldb
http://download.csdn.net/download/fhqsse220/5602861
- tar xvzf MySQL-python-1.2.3.tar.gz
- cd MySQL-python-1.2.3
- vim site.cfg
- 把上面的mysql_config路径注释删除以及改成实际路径,如果不知道mysql_config在哪里,运行命令:whereis mysql_config
- python setup.py install
安装mysqldb老是报错:
- running build
- running build_py
- copying MySQLdb/release.py -> build/lib.linux-x86_64-2.7/MySQLdb
- running build_ext
- building '_mysql' extension
- gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -Dversion_info=(1,2,3,'final',0) -D__version__=1.2.3 -I/usr/include/mysql -I/usr/local/include/python2.7 -c _mysql.c -o build/temp.linux-x86_64-2.7/_mysql.o -g -pipe -m64 -fPIC -g -static-libgcc -fno-omit-frame-pointer -fno-strict-aliasing -DMY_PTHREAD_FASTMUTEX=1
- 在包含自 _mysql.c:36 的文件中:
- /usr/include/mysql/my_config.h:422:1: 警告:“HAVE_WCSCOLL”重定义
- 在包含自 /usr/local/include/python2.7/Python.h:8 的文件中,
- 从 pymemcompat.h:10,
- 从 _mysql.c:29:
- /usr/local/include/python2.7/pyconfig.h:890:1: 警告:这是先前定义的位置
- gcc -pthread -shared build/temp.linux-x86_64-2.7/_mysql.o -L/usr/lib64 -lmysqlclient -lpthread -lm -lrt -ldl -o build/lib.linux-x86_64-2.7/_mysql.so
- /usr/bin/ld: cannot find -lmysqlclient
- collect2: ld 返回 1
说找不到 mysqlclient ,真心不可能,我把mysql-devel 、 mysql-client、mysql-shared都装上了。
只好从系统查找该文件,再建立软连接
- find / -name 'libmysqlclient.so.*'
- /usr/lib64/libmysqlclient.so.14
- /usr/lib64/libmysqlclient.so.12
- /usr/lib64/libmysqlclient.so.14.0.0
- /usr/lib64/libmysqlclient.so.12.0.0
- /usr/lib64/libmysqlclient.so.16
- /usr/lib64/libmysqlclient.so.15
- /usr/lib64/libmysqlclient.so.15.0.0
- /usr/lib64/libmysqlclient.so.16.0.0
建立软连接
- ln -s /usr/lib64/libmysqlclient.so.16.0.0 /usr/lib/libmysqlclient.so
再次install
- python setup.py install
如果这个步骤没有报错,表示基本完成
进入python命令行模式
- python
再import MySQLdb,注意大小写呢
- Python 2.7.3 (default, Dec 10 2012, 14:33:49)
- [GCC 4.1.2 20080704 (Red Hat 4.1.2-51)] on linux2
- Type "help", "copyright", "credits" or "license" for more information.
- >>> import MySQLdb
- /usr/local/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-linux-x86_64.egg/_mysql.py:3: UserWarning: Module _mysql was already imported from /usr/local/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-linux-x86_64.egg/_mysql.pyc, but /home/soft/MySQL-python-1.2.3 is being added to sys.path
完成!