Python3 + MySql: Error loading MySQLdb module: No module named 'MySQLdb'
Solution 1
I would need to see your DATABASES configuration in settings.py, but it looks like it is trying to load MySQLDB instead of the Mysql Python Connector that you installed. The DATABASES should look something like this:
DATABASES = {
'default': {
'NAME': 'mydatabase',
'ENGINE': 'mysql.connector.django',
'USER': 'myuser',
'PASSWORD': 'secretpassword',
'OPTIONS': {
'autocommit': True,
},
}
}
Note the ENGINE part... that tells Django to use the mysql connector instead of MySQLDB...
more info available here: http://bunwich.blogspot.com/2014/02/finally-mysql-connector-that-works-with.html http://dev.mysql.com/doc/connector-python/en/connector-python-django-backend.html
and if you are expecting to use South:
www.pythonanywhere.com/wiki/UsingMySQL
You may want to note that the Oracle connecter is GPL, and there may be license issues with using that code. See Here:
groups.google.com/forum/#!topic/django-developers/8r_RVmUe5ys
The Django 1.7 documentation recommends using the mysqlclient driver...
docs.djangoproject.com/en/1.7/ref/databases/ --see the section on Mysql DB API Drivers
pypi.python.org/pypi/mysqlclient for that client...
-Scott
Solution 2
None of the existing answers worked for me on Ubuntu Server 16.04 so I ran:
sudo apt-get install libmysqlclient-dev
sudo -H pip3 install mysqlclient
The first command get me the mysql config needed by the second command.
Solution 3
pip install mysqlclient
works for me python3.5
Solution 4
If you can install pymysql -- which works well for me with Python 3.4 -- then add these lines to your manage.py file in Django:
try:
import pymysql
pymysql.install_as_MySQLdb()
except ImportError:
pass
This will make pymysql function like MySQLdb and worked for me.
Solution 5
If you're like me, you're working with a Linux install, and Linux still needs Python 2.7 in most cases. That means that
pip install mysqlclient
will install the MySQL client for 2.7, which is segmented from Python 3. To make it install for 3.x you need to use
pip3 install mysqlclient
I did not have to install any packages, but mysql-common
was already installed (Raspbian install) so your mileage may vary there
Related videos on Youtube
skaz
Senior Software Freelancer. Computer Linguistic Master Student in Germany. Feel free to contact me with remote part time opportunities.
Updated on July 08, 2021Comments
-
skaz almost 3 years
I am new to Python and trying to setup a Django project to work with MySql. I have read through the documentation as well as some other StackOverflow posts about the topic, but I still can't get it to work.
When I try to run a migrate in Django I get the following error:
Error loading MySQLdb module: No module named 'MySQLdb'
I have installed the recommended MySql Python Connector (2.0.1) selecting Ubuntu (since I am on Mint Linux). It installs correctly. I still get the error. I don't need to add this library to the project or anything, right? It looks like python should just be aware of this and run successfully. What can I do? Thanks.
EDIT: I forgot to mention: I am running Python 3.4 - a lot of typical solutions are still using 2.7, so they don't work and all the solutions with 3.0 I have tried without success.
-
skaz over 9 yearsI had luck using: github.com/nakagami/django-cymysql But I would prefer to use the "official" connector. Or am I thinking about this wrong?
-
madzohan over 9 yearsThere are a lot of similar questions here "python3 mysql" stackoverflow.com/search?q=python3+mysql I recommend you try github.com/davispuh/MySQL-for-Python-3
-
madzohan over 9 yearspossible duplicate of Can I use MySQL on Django(dev 1.6.x) with Python3.x?
-
skaz over 9 yearsI've tried to make it clear that I have searched these answers and the recommended answers didn't work for me.
-
Omkar Darves over 4 yearsI have already answered this , refer following link: stackoverflow.com/a/58084705/9974063
-
-
e18r about 9 yearsdjango.core.exceptions.ImproperlyConfigured: 'mysql.connector.django' isn't an available database backend. Try using 'django.db.backends.XXX', where XXX is one of: 'mysql', 'oracle', 'postgresql_psycopg2', 'sqlite3'
-
Nicholas Morley over 7 yearsThis solved the problem for me locally but when I deploy it to Elastic Beanstalk I get the old 'No module named 'MySQLdb'' again.
-
Michael Scott Asato Cuthbert over 6 yearsshould no longer be necessary in recent versions of Django.
-
addmoss over 4 yearsWorked for me in Python 3.7 and django 2.2.1 but I've added the import lines in the project's init.py file. (server OS Red Hat 4.8).
-
hypd09 about 4 yearsThis works but if you get a version error: stackoverflow.com/a/59591269/1428624
-
Machavity almost 4 yearsThere's a MAJOR caveat here. In most Linux systems you'll have Python 3.x AND Python 2.7. As such
pip
will attempt to install on 2.7 (which won't help you with Python 3). You'll need to usepip3
to get it into Python 3, as Dan-Dev's answer mentions -
chembrad over 3 yearsThis still works for me on a fresh install of Ubuntu 20.04.