Yeshwanth Venkatesha
Yeshwanth Venkatesha

Reputation: 85

ImportError: No module named 'MySQLdb'

I did a lot of research but couldn't find any solution for this I keep getting this error:

Traceback (most recent call last):
File "test.py", line 2, in <module>
  import MySQLdb
ImportError: No module named 'MySQLdb'

I installed this as well: and got this.

yesh@Yesh:~/DBMS$ sudo apt-get install python-mysqldb
Reading package lists... Done
Building dependency tree       
Reading state information... Done
python-mysqldb is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

Someone please help me with this. I guess it has something to do with environment variables. I don't know exactly what that is. Thanks in advance

Upvotes: 0

Views: 2224

Answers (1)

Luke Dupin
Luke Dupin

Reputation: 2305

First ensure you have a python mysql driver installed:

sudo pip install pymysql

Add this to your settings.py:

# For PyMySQL
try:
  import pymysql
  pymysql.install_as_MySQLdb()
except ImportError:
  pass

You can test that it works by trying this:

$ ipython
import MySQLdb  #Confirm you get an error

import pymysql
pymysql.install_as_MySQLdb()
import MySQLdb  #No error

Upvotes: 2

Related Questions