django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

Each Answer to this Q is separated by one/two green lines.

The problem Im facing while trying to connect to database for mysql. I have also given the database settings that i have used.

 Traceback (most recent call last):
 File "", line 10, in <module>
 File "/home/ar/Desktop/test/testprac/local/lib/python2.7/site-packages/django/core/management/", line 453, in execute_from_command_line
File "/home/ar/Desktop/test/testprac/local/lib/python2.7/site-packages/django/core/management/", line 392, in execute
File "/home/ar/Desktop/test/testprac/local/lib/python2.7/site-packages/django/core/management/", line 272, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/home/ar/Desktop/test/testprac/local/lib/python2.7/site-packages/django/core/management/", line 77, in load_command_class
module = import_module('' % (app_name, name))
File "/home/arundhati/Desktop/test/testprac/local/lib/python2.7/site-packages/django/utils/", line 35, in import_module
File "/home/ar/Desktop/test/testprac/local/lib/python2.7/site-packages/django/core/management/commands/", line 8, in <module>
from import custom_sql_for_model, emit_post_sync_signal
File "/home/ar/Desktop/test/testprac/local/lib/python2.7/site-packages/django/core/management/", line 9, in <module>
from django.db import models
File "/home/ar/Desktop/test/testprac/local/lib/python2.7/site-packages/django/db/", line 40, in <module>
backend = load_backend(connection.settings_dict['ENGINE'])
File "/home/ar/Desktop/test/testprac/local/lib/python2.7/site-packages/django/db/", line 34, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/home/ar/Desktop/test/testprac/local/lib/python2.7/site-packages/django/db/", line 93, in __getitem__
backend = load_backend(db['ENGINE'])
File "/home/ar/Desktop/test/testprac/local/lib/python2.7/site-packages/django/db/", line 27, in load_backend
return import_module('.base', backend_name)
File "/home/ar/Desktop/test/testprac/local/lib/python2.7/site-packages/django/utils/", line 35, in import_module
File "/home/ar/Desktop/test/testprac/local/lib/python2.7/site-packages/django/db/backends/mysql/", line 17, in <module>
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

Databse Settings::

    'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'ar_test_db',                      # Or path to database file if using 
        # The following settings are not used with sqlite3:
        'USER': '',
        'PASSWORD': '',
        'HOST': '',                      # Empty for localhost through domain sockets or   '' for localhost through TCP.
        'PORT': '',                      # Set to empty string for default.

Thanks a lot for the help !!

It looks like you don’t have the python mysql package installed, try:

pip install mysql-python

or if not using a virtual environment (on *nix hosts):

sudo pip install mysql-python

If you get errors trying to install mysqlclient with pip, you may lack the mysql dev library. Install it by running:

apt-get install libmysqlclient-dev

and try again to install mysqlclient:

pip install mysqlclient

you have to install python-mysqldb – Python interface to MySQL

sudo apt-get install python-mysqldb

My answer is similar to @Ron-E, but I got a few more errors/corrections so I’m putting my steps below for Mac OSX on Mavericks and Python 2.7.6.

  1. Install Python mysql package (if you get a success message, then ignore the below steps)

    pip install mysql-python
  2. When I did the above, I got the error “EnvironmentError: mysql_config not found”
    enter image description here
    To fix this, I did the below in terminal:

    export PATH=$PATH:/usr/local/mysql/bin
  3. When I reran step 1, I get a new error “error: command ‘cc’ failed with exit status 1”
    enter image description here
    To fix this, I did the below in terminal:

     export CFLAGS=-Qunused-arguments
     export CPPFLAGS=-Qunused-arguments
  4. I reran step 1 and got the success message ‘Successfully installed mysql-python’!

When I set up Django development environment for PyCharm in Mac OS X Mountain Lion with python, mysql, sequel pro application I got error same as owner of this thread.
However, my answer for them who is running python-mysqldb under Mac OS Mountain Lion x86_x64 (MySql and Python also should be same architecture) and already tried everything like pip and etc. In order fix this problem do following steps:

  1. Download MySql for Python from here
  2. Untar downloaded file. In terminal window do following: tar xvfz downloade.tar.
  3. cd /to untared directory
  4. Run sudo python install
  5. If you get error something like this: “Environment Error: /usr/local/bin/mysql_config not found” then try to add path ass follows: “export PATH=$PATH:/usr/local/mysql/bin”. But id did not helped to me and I found another solution. In the end of command execution error output which looks like this:

    File “/path_to_file/MySQL-python-1.2.4b4/”, line 25, in mysql_config
    raise EnvironmentError(“%s not found” % (mysql_config.path,))

  6. Open with vim and go to line 25 (In your case it can be different unless if it is same version).

  7. Line 25 should look like this after your editing unless your mysql have symbolic link like follows ‘/usr/local/mysql/bin/’:

    f = popen("%s --%s" % ('/usr/local/mysql/bin/mysql_config', what))

  8. After this I got another error as following:

    django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: dlopen(/Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.8-intel.egg/, 2): Library not loaded: libmysqlclient.18.dylib
    Referenced from: /Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.8-intel.egg/
    Reason: image not found

  9. Finally I did following in console:

    sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

Currently everything works fine. So I hope it will be helpful for somebody who uses Mac. 🙂

You are missing the python mysqldb library. Use this command (for Debian/Ubuntu) to install it:
sudo apt-get install python-mysqldb

It is because it did not find sql connector. try:

pip install mysqlclient

I’ve solved this issue in this environment:

$ pyenv --version
pyenv 1.2.9

$ python --version
Python 3.7.2

$ python -m django --version


    'default': {
        'NAME': 'my_db_name',
        'ENGINE': 'mysql.connector.django',   # 'django.db.backends.mysql'
        'USER': '<user>',
        'PASSWORD': '<pass>',
        'HOST': 'localhost',
        'PORT': 3306,
        'OPTIONS': {
            'autocommit': True,

If you use 'ENGINE': 'mysql.connector.django' , install driver executing:

$ pip install mysql-connector-python
Successfully installed mysql-connector-python-8.0.15 protobuf-3.7.0 six-1.12.0

Note that $ pip install mysql-pythondidn’t work for me.

Note that if you use 'ENGINE': 'django.db.backends.mysql' , you should install driver executing:
$ pip install mysqlclient

Finally execute:
$ python migrate

If it’s all right, python creates all these tables id database:


If you are using Python version 3.4 or above, you have to install

sudo apt-get install python3-dev libmysqlclient-dev 

in your terminal. Then install pip install mysqlclient on your virtual env or where you installed pip.

Download and install Mysql-python from here for windows environment.

For Ubuntu 16.04 and 18.04 or python 3 versions

sudo apt-get install python3-mysqldb

On Ubuntu it is advised to use the distributions repository.

sudo apt-get install python-mysqldb


pip install mysqlclient

I recommend to use it in a virtual environment.

With the same error message as Will, it worked for me to install mysql first as the missing file will be added during the installation.
So after

brew install mysql

pip install mysql-python

ran without errors.

This happened with me as well and I believe this has become a common error not only for Django developers but also for Flask as well, so the way I solved this issue was using brew.

  1. brew install mysql
  2. sudo pip install mysql-python

This way every single issue was solved and both frameworks work absolutely fine.

P.S.: For those who use macports (such as myself), this can be an issue as brew works in a different level, my advice is to use brew instead of macports

I hope I could be helpful.

I think it is the version error.

try installing this in following order:

  1. sudo apt-get install python3-mysqldb

  2. pip3 install mysqlclient

  3. python3 makemigrations

  4. python3 migrate

I was having the same problem.The following solved my issue

Run pip install pymysql in your shell

Then, edit the file in your project origin directory(the same as
and then


import pymysql


this should solve the problem.

Just to add to other answers, if you’re using Django, it is advisable that you install mysql-python BEFORE installing Django.

I wasted a lot of time on this. Turns out that the default database library is not supported for Python 3. You have to use a different one.

if the error looks like this

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module:
dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/, 2): Library not loaded: 
Referenced from: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/

then try :

pip install python-mysqldb

Faced similar issue. I tried installing mysql-python using pip, but it failed due to gcc dependency errors.

The solution that worked for me

conda install mysql-python

Please note that I already had anaconda installed, which didn’t had gcc dependency.

I faced this similar error to when I wanted to use MySQL as the default database.
These error might be as a result of the fact that mysqlclient hasn’t been installed.
And this can be installed from the command line using;
pip install mysqlclient (for recent versions of python)
or pip install mysql-python (for older versions of python)
In my case, after meeting this requirement, I still had this same error and it was solved as below;
In your django project directory, select venv directory and under these directory,
select the pyvenv.cfg file and click allow on the dialog box which ask you if you want to edit this file. In this file,
include-system-site-packages is set to false. So change this line of code to true like this;

include-system-site-packages = true

Everything now works well

try this, simply using pip:
On Windows the recommended command is:

python -m pip install mysqlclient

use it in a virtual environment

I was stuck in this for a very long and then I realized what’s wrong django checks for mysqlclient if it’s not installed before django it throws this error so simple solution is

pip uninstall django
pip install mysqlclient
pip install django


Install (if not found):

pip install mysql.connector


'default': {
    'ENGINE': 'mysql.connector.django',
    'NAME': '...',
    'HOST': '...',
    'PORT': '3306',
    'USER': '...',
    'PASSWORD': '...',

Try this

apt-get install libmysqlclient-dev

and try again to install mysqlclient:

pip install mysqlclient

Maybe you can try the following mode of operation:
sudo python runserver

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .