准备使用python写点东西,又要依赖mysql,于是找到相关的模块MySQLdb准备安装。
1,在windows环境下,使用google搜索 python mysql windows几个关键词,找到.exe的安装包,安装非常容易。如果想挑战自己也可以通过tar包去安装,我自己安装过程中会遇到一些问题,关于setuptools的依赖之类的,这个问题后面再说,反正一句话,windows下就找.exe的安装包,图形界面,点击即可
2,linux下,按照说明打开下载到的MySQL-python-1.2.3.tar.gz,执行python setup.py install会遇到说setuptools没有安装的问题,于是准备安装之
3,下载setuptools-0.6c11.tar.gz,解压缩,同时下载到setuptools-0.6c11-py2.4.egg(选择与系统安装的python版本号对应的文件,我这里系统安装的python是2.4)
按照说明执行:
sh setuptools-0.6c11-py2.4.egg
发现又是缺东西
>>> from distutils.util import convert_path
Traceback (most recent call last):
File "
ImportError: No module named distutils.util
>>>
又是一番Google,得知是distutils模块没安装,这主要是和系统安装时,选择的安装包有关系,没有按转python-dev的rpm于是找到安装(有依赖关系),下面给出依赖的安装顺序:
sudo rpm -ivh blt-2.4z-222.2.i586.rpm
sudo rpm -ivh python-tk-2.4.2-18.25.i586.rpm
sudo rpm -ivh python-devel-2.4.2-18.25.i586.rpm
安装好后,再去执行
sh setuptools-0.6c11-py2.4.egg
又报错:
The following error occurred while trying to add or remove files in the
installation directory:
[Errno 2] No such file or directory: '/usr/local/lib/python2.4/site-packages/test-easy-install-5157.pth'
The installation directory you specified (via --install-dir, --prefix, or
the distutils default setting) was:
/usr/local/lib/python2.4/site-packages/
经过Google外加核对setuptools的安装文档,发现是没有指定安装目录的原因,于是用root把它装在python的系统库目录:
sh setuptools-0.6c11-py2.4.egg --prefix=/usr
显示信息如下:
Processing setuptools-0.6c11-py2.4.egg
creating /usr/lib/python2.4/site-packages/setuptools-0.6c11-py2.4.egg
Extracting setuptools-0.6c11-py2.4.egg to /usr/lib/python2.4/site-packages
Adding setuptools 0.6c11 to easy-install.pth file
Installing easy_install script to /usr/bin
Installing easy_install-2.4 script to /usr/bin
Installed /usr/lib/python2.4/site-packages/setuptools-0.6c11-py2.4.egg
Processing dependencies for setuptools==0.6c11
Finished processing dependencies for setuptools==0.6c11
终于完成了setuptools的正确安装,不容易啊!别忘了,我就是想装个MySQLdb。。。
4,安装MySQLdb
首先编辑解压包中的site.cfg文件,设置mysql_config文件所在的正确目录(mysql安装后,bin目录下都会有mysql_config文件的)
mysql_config = /usr/local/mysql/bin/mysql_config
保存后执行:
python setup.py install --prefix=/usr
这样MySQLdb模块就被安装上了
5,测试效果
打开python交互解释器,输入:
import MySQLdb
又报错了!!!
Traceback (most recent call last):
File "test_db.py", line 1, in ?
import MySQLdb
File "/usr/lib/python2.4/site-packages/dbus/__init__.py", line 19, in ?
File "build/bdist.linux-i686/egg/_mysql.py", line 7, in ?
File "build/bdist.linux-i686/egg/_mysql.py", line 6, in __bootstrap__
ImportError: libmysqlclient.so.18: cannot open shared object file: No such file or directory
熟悉C/C++编程的一定知道问题所在了,就是LD_LIBRARY_PATH的问题了,python不知道去哪里链接mysql的so了,好办
export LD_LIBRARY_PATH=/usr/local/mysql/lib:$LD_LIBRARY_PATH
然后再去测试,一切正常了!过程太曲折了,文字记在这里,希望也能帮到别人