How do I Install MySQLdb in Python?

PythonServer Side ProgrammingProgramming

Before proceeding, you make sure you have MySQLdb installed on your machine. Just type the following in your Python script and execute it −

import MySQLdb

If it produces the following result, then it means MySQLdb module is not installed −

Traceback (most recent call last):
   File "", line 3, in <module>
      import MySQLdb
ImportError: No module named MySQLdb

To install MySQLdb module, use the following command −

For Ubuntu, use the following command -
$ sudo apt-get install python-pip python-dev libmysqlclient-dev
For Fedora, use the following command -
$ sudo dnf install python python-devel mysql-devel redhat-rpm-config gcc
For Python command prompt, use the following command -
pip install MySQL-python

Note − Make sure you have root privilege to install above module.

Updated on 31-Jan-2020 09:43:48