ImportError: No module named MySQLdb (Python)

0 min read 170 words

Trying to use MySQL with Python and getting an ImportError?

Traceback (most recent call last):
 File "some_file.py", 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: https://github.com/PyMySQL/mysqlclient-python

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:

Debian/Ubuntu:

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

RedHat/CentOS

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

Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags