django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb
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 "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/ar/Desktop/test/testprac/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 453, in execute_from_command_line
utility.execute()
File "/home/ar/Desktop/test/testprac/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/ar/Desktop/test/testprac/local/lib/python2.7/site-packages/django/core/management/__init__.py", 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/__init__.py", line 77, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/home/arundhati/Desktop/test/testprac/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/home/ar/Desktop/test/testprac/local/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 8, in <module>
from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal
File "/home/ar/Desktop/test/testprac/local/lib/python2.7/site-packages/django/core/management/sql.py", line 9, in <module>
from django.db import models
File "/home/ar/Desktop/test/testprac/local/lib/python2.7/site-packages/django/db/__init__.py", 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/__init__.py", 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/utils.py", line 93, in __getitem__
backend = load_backend(db['ENGINE'])
File "/home/ar/Desktop/test/testprac/local/lib/python2.7/site-packages/django/db/utils.py", 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/importlib.py", line 35, in import_module
__import__(name)
File "/home/ar/Desktop/test/testprac/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", 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::
DATABASES =
'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 '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
Thanks a lot for the help !!
20 Answers
20
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
I am using virtual env, and i have installed mysql, django... however when i do python manage.py syncdb It gives me the same error that i have posted
– getitstarted
Mar 9 '13 at 23:13
do i have a version problem by any chance? I am using django 1.5 , python 2.7.3 with my sql(14.14 for debian)
– getitstarted
Mar 10 '13 at 0:57
This solve for me: Activate my environment # source bin/activate Then install it as a package # sudo apt-get install python-mysqldb
– leticia
Apr 27 '13 at 2:25
with python 3 is: pip install mysqlclient
– sadalsuud
Dec 27 '18 at 16:01
you have to install python-mysqldb - Python interface to MySQL
Try sudo apt-get install python-mysqldb
sudo apt-get install python-mysqldb
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
It was
pip install mysqlclient. I had forgotten to add this to my requirements.txt file, so even though the MySQL database was there and populated, my Django web server couldn't interact with it.– Blairg23
Sep 10 '17 at 11:13
pip install mysqlclient
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:
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/setup_posix.py", line 25, in mysql_config
raise EnvironmentError("%s not found" % (mysql_config.path,))
Open setup_posix.py with vim and go to line 25 (In your case it can be different unless if it is same version).
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))
f = popen("%s --%s" % ('/usr/local/mysql/bin/mysql_config', what))
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/_mysql.so, 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/_mysql.so
Reason: image not found
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. :)
Thanks a lot. It took me almost a week just to run my first project using mysql haha. Phew...
– Avigit
Mar 31 '15 at 19:54
Thanks a ton man! you saved my day! i was using mac and getting same problem.
– user2772346
Jul 24 '15 at 14:05
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.
Install Python mysql package (if you get a success message, then ignore the below steps)
pip install mysql-python
When I did the above, I got the error "EnvironmentError: mysql_config not found"

To fix this, I did the below in terminal:
export PATH=$PATH:/usr/local/mysql/bin
When I reran step 1, I get a new error "error: command 'cc' failed with exit status 1"
To fix this, I did the below in terminal:
export CFLAGS=-Qunused-arguments
export CPPFLAGS=-Qunused-arguments
I reran step 1 and got the success message 'Successfully installed mysql-python'!
Thanks for suggestion. I got first of your problem.
– user2772346
Jul 24 '15 at 13:42
I have followed your way but first error is always occurring in my case. And for ` export CFLAGS=-Qunused-arguments ` terminal is giving that error:
-bash: export: =-Qunused-arguments': not a valid identifier – RegarBoy
Nov 25 '15 at 3:46
-bash: export: =-Qunused-arguments': not a valid identifier
You are missing the python mysqldb library. Use this command (for Debian/Ubuntu) to install it:sudo apt-get install python-mysqldb
mysqldb
sudo apt-get install python-mysqldb
This is an answer, but I think it would be great if you could add more details than a single line. Thanks!
– Uli Köhler
Jan 29 '14 at 0:34
Above command help us to install the Python interface to the MySQL database.
– Haimei
Jan 29 '14 at 1:51
Download and install Mysql-python from here for windows environment. http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python.
I used this too. It worked.
– Jubayer Arefin
Nov 20 '13 at 9:31
I use this, and I dont' install mysql my machine
– tyan
Mar 19 '18 at 3:24
On Ubuntu it is advised to use the distributions repository.
sudo apt-get install python-mysqldb
It is because it did not find sql connector. try:
pip install mysqlclient
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.
brew install mysql
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.
For Ubuntu 16.04 and 18.04 or python 3 versions
sudo apt-get install python3-mysqldb
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.
Command "/usr/bin/python -u -c "import setuptools, tokenize;__file__='/private/var/folders/yn/nrq74lgs6kqdzcr035p_5g280000gn/T/pip-build-jwQcm5/mysql-python/setup.py';f=getattr(tokenize, 'open', open)(file);code=f.read().replace('rn', 'n');f.close();exec(compile(code, file, 'exec'))" install --record /var/folders/yn/nrq74lgs6kqdzcr035p_5g280000gn/T/pip-UmTWRJ-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /private/var/folders/yn/nrq74lgs6kqdzcr035p_5g280000gn/T/pip-build-jwQcm5/mysql-python/
– tisuchi
Nov 7 '17 at 4:34
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/_mysql.so, 2): Library not loaded:
/usr/local/opt/mysql/lib/libmysqlclient.20.dylib
Referenced from: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/_mysql.so
then try :
pip install python-mysqldb
Faced similar issue. I tried installing mysql-python using pip, but it failed due to gcc dependency errors.
mysql-python
The solution that worked for me
conda install mysql-python
Please note that I already had anaconda installed, which didn't had gcc dependency.
Package not found errors.
– yubaraj poudel
Nov 29 '17 at 8:24
could you elaborate on it
– Ayush Vatsyayan
Dec 15 '17 at 6:28
I was having the same problem.The following solved my issue
Run pip install pymysql in your shell
Then, edit the init.py file in your project origin directory(the same as settings.py)
and then
add:
import pymysql
pymysql.install_as_MySQLdb()
this should solve the problem.
"Then, edit init.py file in your project origin directory" - do you mean
__init__.py ?– Alex Yu
Feb 13 at 5:19
__init__.py
If you are using pyhton version 3.4 or above. you have to install
sudo apt-get install python3-dev libmysqlclient-dev
in terminal. then install pip install mysqlclient on your virtual env or where you installed pip.
pip install mysqlclient
Maybe you can try the following mode of operation:sudo python manage.py runserver 0.0.0.0:8000
sudo python manage.py runserver 0.0.0.0:8000
Seems like you don't have permission to the Python folder. Try sudo chown -R $USER /Library/Python/2.7
sudo chown -R $USER /Library/Python/2.7
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Are you using a virtual environment? I recommend you do just to keep things organized. If that's not an option then you'll need to run pip with superuser privileges: sudo pip install mysql-python
– Ron E
Mar 9 '13 at 17:17