ImportError: No module named MySQLdb [Solved]

Question: My Python script failed with an error message “ImportError: No module named MySQLdb” while performing MySQL operation. Here’s the complete error message.

$python comments.sql
Traceback (most recent call last):
 File "", line 7, in <module>
 import MySQLdb
ImportError: No module named MySQLdb

How to fix this error?

Solution: Fix ImportError: No module named MySQLdb

The error message indicates that there is no module named MySQLdb. It means, Python needs a module to interface with MySQL database and that modules does not seems to be present in the system. All you need to do is, just install MySQL-Python module and the script should work without any problem.

You can use pip to install any Python module. If you don’t have python-pip installed, then you may follow python pip installation guide.

Using Python Pip

# pip install MySQL-python
Collecting MySQL-python
Installing collected packages: MySQL-python
 Running install for MySQL-python
Successfully installed MySQL-python-1.2.5

MySQL-Python module can be installed using apt-get, yum or easy_install (depends on your Linux distribution)

On Ubuntu based systems:

# apt-get install python-mysqldb

On RHEL/CentOS based systems:

# yum install MySQL-python

Using easy_install

# easy_install mysql-python

That’s it!

Other no module named errors & fix

Updated on November 1, 2017

Was this article helpful?

Related Articles


Leave a Comment