1,下载最新的MySQLdb安装包:
tar zxvf MySQL-python-1.2.3.tar.gz
cd MySQL-python-1.2.3
2,进入用户MySQLdb源码目录,运行 python setup.py build。
报错:ImportError: No module named setuptools
解决:安装setuptools
wget https://pypi.python.org/packages/source/s/setuptools/setuptools-19.2.tar.gz#md5=78353b1f80375ca5e088f4b4627ffe03
tar zxvf setuptools-19.2.tar.gz
cd setuptools-19.2
python setup.py build
sudo python setup.py install
3,进入用户MySQLdb源码目录,运行python setup.py install
4,继续,安装,安装完毕
>>> import MySQLdb
>>>
如果报错:ImportError: libmysqlclient.so.18: cannot open shared objectfile: No such file or directory
如果是64系统则:
#ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18
再次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 /mnt/MySQL-python-1.2.3 is being added to sys.path
原因是,install后MySQLdb模块已经被放到python的site-pachages目录中;但我在当前目录也存在相同的模块,所以可能会重复导入。只要切换到其他目录运行就可以了。
本文转自奔跑在路上博客51CTO博客,原文链接http://blog.51cto.com/qiangsh/1733867如需转载请自行联系原作者
qianghong000