[Solved] ModuleNotFoundError: No module named MySQLdb

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

Step 2: Install “mysqlclient” Package

pip3 install mysqlclient

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.

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

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

Related Posts