报错信息:
ImportError: No module named MySQLdb
1,下载最新的MySQLdb安装包:
官方下载地址:https://pypi.org/project/MySQL-python/
wget http://sourceforge.net/projects/mysql-python/files/mysql-python/1.2.5/MySQL-python-1.2.5.tar.gz/download tar zxvf MySQL-python-1.2.5.tar.gz cd MySQL-python-1.2.5 python setup.py build
2,进入用户MySQLdb源码目录,运行 python setup.py build。
报错:ImportError: No module named setuptools
解决:安装setuptools
官方下载地址:https://pypi.org/project/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
如果报错:
Traceback (most recent call last): File "", line 1, in File "/opt/rh/python27/root/usr/lib64/python2.7/site-packages/MySQLdb/__init__.py", line 19, in import _mysql ImportError: libperconaserverclient.so.20: cannot open shared object file: No such file or directory
如果是64系统则:
ln -s /usr/local/mysql5.7/lib/libperconaserverclient.so.20 /usr/lib64/libperconaserverclient.so.20 ln: creating symbolic link `/usr/lib64/libperconaserverclient.so.20': File exists 提示文件已经存在,删掉,在创建软连接 rm -f /usr/lib64/libperconaserverclient.so.20 ln -s /usr/local/mysql5.7/lib/libperconaserverclient.so.20 /usr/lib64/libperconaserverclient.so.20
再次import MySQLdb就正常了。
如果提示如下:
/usr/local/lib/python2.7/site-packages/MySQL_python-1.2.5-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.5-py2.7-linux-x86_64.egg/_mysql.pyc, but /usr/local/MySQL-python-1.2.5 is being added to sys.path
原因是,install后MySQLdb模块已经被放到python的site-pachages目录中;但我在当前目录也存在相同的模块,所以可能会重复导入。只要切换到其他目录运行就可以了。
参考链接:https://blog.51cto.com/qiangsh/1733867