[Solved] ModuleNotFoundError: No module named MySQLdb

MySQL is one of the most efficient to be used in Python. The Python module “mysqldb” allows you to connect your Python to MySQL. If the module is not installed on your system, then the error “no module named ‘mysqldb’” occurs.

ModuleNotFoundError No module named MySQLdb
ModuleNotFoundError: No module named MySQLdb Fix

This post will address the possible reasons and the solutions to fix the error “no module named mysqldb”.

Why this error occurs?

A Python module relies on either a single package or multiple packages. If one of the associated packages of “mysqldb” is not installed, and you are trying to import the module, then the error “no module named mysqldb” occurs.

Here, the snippet shows that we are trying to import but the error occurred:

Error "ModuleNotFoundError"
ModelNotFoundError

Solution: Install “MySQLdb”

The “MySQLdb” module is available through the PIP, a Python-based manager. Ensure that PIP is installed on your system. The complete solution is demonstrated in the following steps:

Step 1: Install PIP

The PIP can be installed on Linux using the following set of commands:

$ sudo apt install python3-pip           #For Debian and Ubuntu-Based Distributions
$ sudo yum install python3-pip           #For CentOS7/RHEL
$ sudo dnf install python3-pip           #For Fedora/CentOS8
$ sudo pacman -S python3-pip             #For Arch-Based Distributions
Installing python3-pip
Installing python3-pip

You need to use the package name “python-pip” to install PIP on Python2.

Step 2: Install “mysqlclient” Package

The “MySQLdb” module is associated with two packages that need to be installed before importing different packages of PIP. The first package is “mysqlclient” which enables Python to connect to MySQL.

The absence of this package is the primary reason you cannot import the module. The “mysqlclient” package can be installed on Linux as:

$ pip3 install mysqlclient
Command to install MySQL package
Install MySQL package

You may receive an error while installing “mysqlclient”. It is because you do not have the

development package of Python to embed the package “mysqlclient” on your system. This set of packages can be installed using the command:

$ sudo apt install python3-dev default-libmysqlclient-dev build-essential
$ sudo yum install python3-dev default-libmysqlclient-dev build-essential
$ sudo dnf install python3-dev default-libmysqlclient-dev build-essential

Note: The “apt”, “yum”, and “dnf” refers to the Debian/Ubuntu, CentOS/RHEL, and Fedora based distributions.

Command to instal MySQL build essentials
MySQL build essentials installation

After installing “mysqlclient,” try to import the module. If the error remains, then proceed further.

Step 3: Install “mysql-connector-python”

This package allows Python to connect to MySQL Databases. The following PIP-based command can be used to install it:

$ sudo pip3 install mysql-connector-python
Command for MySQL to python connector
connecting python to MySQL DB

If you are using PIP for Python2, then replace with package manager name “pip3” to “pip2”

Step 4: Verify the Solution

Now, import the module using its complete name:

>>> import MySQLdb
Importing MySQLdb
Import MySQLdb

The screenshot shows that the module is imported into the Python environment.

The error “no module named mysqldb” occurs when the user tries to import it in the absence of the packages that are required to import this module. The error can be fixed by installing the package “mysqlclient” on the system. However, if it is not fixed yet, then install the “mysql-connector-python” package using the PIP. This post has listed the reason and the solutions to fix the “MySQLdb” module error on Linux.

ABOUT THE AUTHOR

Muhammad Zubyan


Muhammad Zubyan is a certified Google IT Support Professional with over 7 years of extensive experience. He has worked on more than 1500 computers, gaining valuable insights that enable him to detect and troubleshoot any complicated root cause of Windows-related issues and errors. In addition to managing Appuals as a Senior Editor, he is currently developing his own Game Optimization program that caters to both gamers and casual users alike.