Python error "ImportError: No module named MySQLdb" solution


This error may be due not installed MySQL module, execute the following statement to install such a case:

pip install MySQLdb


If you encounter an error during installation "_mysql.c: 29: 20: Fatal error: Python.h: No such file or directory", you should first install the dependent:

yum install -y python-devel


Perform the following command to view the installation MySQL package:

pip show mysql

pip list

yum list MySQL-python


You may also enter the Python Shell to view the current path of the package:

import sys

print sys.path


If you installed after MySQLdb still being given "ImportError: No module named MySQLdb" , probably because the packet routing problem, that is within the actual path is not MySQLdb Python search, where you can manually set the environment variable PYTHONPATH included. Like:

export PYTHONPATH=/usr/lib64/python2.7/site-packages:$PYTHONPATH


Add tags or display package path:

>>> import sys

>>> sys.path.append("/usr/lib64/python2.7/site-packages")

>>> import MySQLdb


To Python-2.7, for example, to package the default path is generally a few:




Guess you like