ImportError: No module named MySQLdb (Python)

Trying to use MySQL with Python and getting an ImportError?

Traceback (most recent call last):
 File "", line 4, in 
 import MySQLdb as mdb
 ImportError: No module named MySQLdb

This is likely because you have not installed it to the system. You can do this on Ubuntu Linux by running the following code:

sudo apt-get install python-mysqldb

Also, remember that there is no MySQLdb for python3.x and above. There is however, a fork of it on GitHub at:

For Mac users

If you want to install MySQL as well:

brew install mysql

Otherwise just install it through PIP:

pip install mysqlclient

For Linux users

pip install mysqlclient

If you need additional development headers, you can also execute:


sudo apt-get install python3-dev default-libmysqlclient-dev build-essential


sudo yum install python3-devel mysql-devel

Have you tried PyMYSQL instead?

PyMySQL is great, you can give it a whirl by installing it with PIP:

pip install pymysql

Sometimes you have to first install Wheel, which you can easily do by running:

pip install wheel

Notify of
Inline Feedbacks
View all comments