No module named MySQLdb

Viewed 833273

I am using Python version 2.5.4 and install MySQL version 5.0 and Django. Django is working fine with Python, but not MySQL. I am using it in Windows Vista.

33 Answers

mysqldb is a module for Python that doesn't come pre-installed or with Django. You can download mysqldb here.

I am at ubuntu (linux) and what worked for me was

sudo apt-get install python3-dev default-libmysqlclient-dev build-essential

and then finally

pip install mysqlclient

for Windows :

pip install mysqlclient pymysql

then:

import pymysql
pymysql.install_as_MySQLdb()

for python 3 Ubuntu

sudo apt-get install -y python3-mysqldb

For anyone coming to this page when trying to find a solution for sqlalchemy, all you need to do is:

pip install PyMySQL

And adjust your connection string to use PyMySQL, from mysql:// to mysql+pymysql://.

For Python 3.6+

sudo apt-get install libmysqlclient-dev
pip3 install mysqlclient

does the trick

Python 3.8

sudo apt-get install libmysqlclient-dev
sudo apt-get install -y python3-mysqldb
pip3 install pymysql

settings.py

import pymysql
pymysql.install_as_MySQLdb()
pip install --user mysqlclient 

above works for me like charm for me.I go the error from sqlalchemy actually. Environment information :

Python : 3.6, Ubuntu : 16.04,conda 4.6.8

I personally recommend using pymysql instead of using the genuine MySQL connector, which provides you with a platform independent interface and could be installed through pip.

And you could edit the SQLAlchemy URL schema like this: mysql+pymysql://username:passwd@host/database

If you are running on Vista, you may want to check out the Bitnami Django stack. It is an all-in-one stack of Apache, Python, MySQL, etc. packaged with Bitrock crossplatform installers to make it really easy to get started. It runs on Windows, Mac and Linux. Oh, and is completely free :)

For Python 3+ version

install mysql-connector as:

pip3 install mysql-connector 

Sample Python DB connection code:

import mysql.connector
db_connection = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd=""
)
print(db_connection)

Output:

> <mysql.connector.connection.MySQLConnection object at > 0x000002338A4C6B00>

This means, database is correctly connected.

On Debian Buster, the following solution worked for me with python 3.7:

sudo apt-get install libmysqlclient-dev
sudo apt-get install libssl-dev
pip install mysqlclient

Python 3

Make sure the import order:

#BAD
import MySQLdb             # <------ NOT here
import pymysql
pymysql.install_as_MySQLdb()

#GOOD
import pymysql
pymysql.install_as_MySQLdb()
import MySQLdb              # <------- HERE!

None of the above worked for me on an Ubuntu 18.04 fresh install via docker image.

The following solved it for me:

apt-get install holland python3-mysqldb

On my mac running Catalina v10.15.2, I had the following MySQLdb version conflict:

ImportError: this is MySQLdb version (1, 2, 5, 'final', 1), but _mysql is version (1, 4, 6, 'final', 0)

To resolve it, I did the following:

pip uninstall MySQL-python
pip install MySQL-python

Faced this issue with mysql.connector.python version 8.0.24 on mac(if code base is same then the issue should happen in windows as well). This file on line 51 imports "from django.db.backends.mysql.base import DatabaseWrapper as MySQLDatabaseWrapper". The imported file has following code 14-20(exact code and error that you received is part of code

try:
    import MySQLdb as Database
except ImportError as err:
    raise ImproperlyConfigured(
        'Error loading MySQLdb module.\n'
        'Did you install mysqlclient?'
    ) from err

The error is formed here. Not sure why this import keeps coming back in different version of mysql connector but 8.0.23 does not have the import, so I reverted to that version and error was gone... This is incase you wish to continue to work with mysql.connector.python

Win10 / Python27 this worked for me:

easy_install mysql-python

all other 'pip install...' failed with dependency errors

When I ran the command apt-get install python-mysqldb, I still encountered the error message below

mysql_config not found when installing mysqldb python interface

So I first had to run the command below

sudo apt-get install libmariadbclient-dev

If you use MySQL instead of MariaDB, run the following instead

Then run

apt-get install python-mysqldb 

Then now run

pip install mysqlclient

For CentOS 8 and Python3

$ sudo dnf install python3-mysqlclient -y

Windows 7 / Python 3.6.5 this works for me:

conda install -y mysqlclient

Related