522

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
jbcedge
  • 18,965
  • 28
  • 68
  • 89

33 Answers33

743

You need to use one of the following commands. Which one depends on what OS and software you have and use.

  1. easy_install mysql-python (mix os)
  2. pip install mysql-python (mix os/ python 2)
  3. pip install mysqlclient (mix os/ python 3)
  4. apt-get install python-mysqldb (Linux Ubuntu, ...)
  5. cd /usr/ports/databases/py-MySQLdb && make install clean (FreeBSD)
  6. yum install MySQL-python (Linux Fedora, CentOS ...)

For Windows, see this answer: Install mysql-python (Windows)

yeeking
  • 958
  • 8
  • 11
derevo
  • 9,048
  • 2
  • 22
  • 19
  • 1
    I had to update my version of setuptools to match my updated version of python on Mac OS before I could get this to work. – Jonathan Adelson Dec 21 '11 at 23:57
  • I had several problems with `easy_install`, I think **ActivePython** is very reliable than `easy_install`. – Afshin Mehrabani Oct 04 '12 at 11:36
  • 4
    and how about install on virtualenv with python 3 on it? I used: pip install mysql-python only to get: ImportError: No module named ConfigParser – Iliyan Bobev Aug 27 '13 at 10:48
  • 11
    In Python 3, ConfigParser has been renamed to configparser for PEP 8 compliance. It looks like the package you are installing does not support Python 3. MySQL-python does not currently support Python 3. – derevo Aug 28 '13 at 07:51
  • 1
    See this question for some options in Python3: https://stackoverflow.com/questions/4960048/python-3-and-mysql/25724855#25724855 – Symmetric Jan 07 '16 at 00:23
  • 7
    I receive this error when using 1 or 2 in `OSX Yosemite`: `EnvironmentError: mysql_config not found` – Mona Jalal Jun 30 '16 at 20:38
  • easy_install worked for me on macOS High Sierra (v10.13.2) using Python v2.7.14 – Lee Jan 30 '18 at 13:06
  • 3
    FYI - I am on ubuntu 18 and using python3.7. After doing "pip install mysqlclient", I needed to do "sudo apt-get install libmysqlclient-dev" for successful installation. – Rashmi Singh Nov 19 '19 at 09:09
  • If you are using Python3 on Ubuntu, it is always recommended to update your repositories before installing a new package so you run "sudo apt-get update" and then you install the package by running "sudo apt-get install python3-mysqldb" and you are good to go. – John Jan 04 '21 at 08:06
  • I tried `yum install MySQL-python` on my dockerfile (with Python 3.8), but error `No module named 'MySQLdb'` still exists, does anyone know how to resolve it? – wawawa Jun 02 '21 at 14:53
  • 3. pip install mysqlclient (mix os/ python 3) This worked for me on Ubuntu with python3.8.5. – iconique Jun 23 '21 at 03:11
176

...and remember there is no MySQLdb for python3.x

(I know the question is about python2.x but google rates this post quite high)


EDIT: As stated in the comments, there's a MySQLdb's fork that adds Python 3 support: github.com/PyMySQL/mysqlclient-python

Janek Olszak
  • 4,067
  • 1
  • 28
  • 22
  • Urghhgwaarrbl... there goes my hopes of using python3 in my Django app – Don Cheadle Feb 02 '15 at 16:45
  • 7
    Luckily, there is a selection of alternatives, many of which use the same API: [Python 3 and MySQL](http://stackoverflow.com/questions/4960048/python-3-and-mysql) – Anthony Geoghegan Mar 24 '15 at 10:07
  • 4
    [mysqlclient](https://pypi.python.org/pypi/mysqlclient) is a fork of MySQLdb that works well with Python 3. – ostrokach Feb 04 '16 at 23:57
  • @JanekOlszak I did not know this - only took about a day of lots of cursing and banging on the keyboard - until reading this and swapping over to 2.7. And guess what, everything works hunky-dory~ – 3kstc Aug 11 '16 at 01:14
  • My journey, this -> [this](https://stackoverflow.com/a/21530768/812919) -> [this](https://stackoverflow.com/a/53392907/812919). And then, I was finally able to successfully install it. – olfek Feb 23 '19 at 15:00
  • Install mysql dev files: `sudo apt-get install libmysqlclient-dev` followed by: `pip install mysqlclient` – Melroy van den Berg Mar 17 '22 at 14:28
98

if your python version is 3.5, do a pip install mysqlclient, other things didn't work for me

goksel
  • 4,450
  • 3
  • 42
  • 51
49

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

Kenan Banks
  • 207,056
  • 34
  • 155
  • 173
Evan Fosmark
  • 98,895
  • 36
  • 105
  • 117
  • 1
    Trying to install MySQL-python-1.2.1 I get the message **ImportError: No module named setuptools** Trying to install setuptools, I get **zlib not available** isn't it supposed to be bundled with python ? – Pierre de LESPINAY May 02 '11 at 09:30
  • after downloading the module you'll need to edit the site.cfg file with the right path for mysql_config and the run `python setup.py install` – ntanase Mar 08 '14 at 23:44
37

Note this is not tested for python 3.x

In CMD

pip install wheel
pip install pymysql

in settings.py

import pymysql
pymysql.install_as_MySQLdb()

It worked with me

Community
  • 1
  • 1
A.Raouf
  • 2,171
  • 1
  • 24
  • 36
35

Ubuntu:

sudo apt-get install python-mysqldb
om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
panckreous
  • 529
  • 6
  • 4
34

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
34

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://.

James Briggs
  • 854
  • 6
  • 13
23

for Windows :

pip install mysqlclient pymysql

then:

import pymysql
pymysql.install_as_MySQLdb()

for python 3 Ubuntu

sudo apt-get install -y python3-mysqldb
cssyphus
  • 37,875
  • 18
  • 96
  • 111
Saurabh Chandra Patel
  • 12,712
  • 6
  • 88
  • 78
20
pip install PyMySQL

and then add this two lines to your Project/Project/init.py

import pymysql
pymysql.install_as_MySQLdb()

Works on WIN and python 3.3+

alphiii
  • 1,597
  • 3
  • 21
  • 27
13

Try this.

pip install MySQL-python
Venkat Kotra
  • 10,413
  • 3
  • 49
  • 53
13

If pip install mysqlclient produces an error and you use Ubuntu, try:

sudo apt-get install -y python-dev libmysqlclient-dev && sudo pip install mysqlclient
Kostyantyn
  • 5,041
  • 3
  • 34
  • 30
13

For Python 3.6+

sudo apt-get install libmysqlclient-dev
pip3 install mysqlclient

does the trick

Umair Ayub
  • 19,358
  • 14
  • 72
  • 146
10

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()
Akash Kumar Verma
  • 3,185
  • 2
  • 16
  • 32
7

I met the same situation under windows, and searched for the solution.

Seeing this post Install mysql-python (Windows).

It points out installing such a pip environment is difficult, needs many other dependencies.

But I finally know that if we use mysqlclient with a version down to 1.3.4, it don't need that requirements any more, so try:

pip install mysqlclient==1.3.4
Community
  • 1
  • 1
Alfred Huang
  • 17,654
  • 32
  • 118
  • 189
6
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

Asraful
  • 1,241
  • 18
  • 31
6

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

Justin Lee
  • 800
  • 1
  • 11
  • 22
5
  • Go to your project directory with cd.
  • source/bin/activate (activate your env. if not previously).
  • Run the command easy_install MySQL-python
fedorqui
  • 275,237
  • 103
  • 548
  • 598
GrvTyagi
  • 4,231
  • 1
  • 33
  • 40
5

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 :)

Daniel Lopez
  • 3,297
  • 2
  • 30
  • 29
  • Then why did you say "If you are running on Vista" if it will work on more than Vista? That's misleading and confusing. – Anthony Dec 22 '13 at 14:41
  • 3
    To Anthony: I just found funny that the top-voted comment provided instructions for Linux, FreeBSD and OSX but not for Vista as specified by the poster – Daniel Lopez Dec 25 '13 at 20:04
4

Thanks to derevo but I think there's another good way for doing this:

  1. Download and install ActivePython
  2. Open Command Prompt
  3. Type pypm install mysql-python
  4. Read the notes specific to this package.

I think pypm is more powerful and reliable than easy_install.

Afshin Mehrabani
  • 33,262
  • 29
  • 136
  • 201
  • Seems like it requires a paid subscription `Can't install mysql-python-1.2.3: requires Business Edition subscription` – Jeremy S. Nov 21 '12 at 13:17
  • I don't think so. I remember I've installed that with no problem. – Afshin Mehrabani Nov 21 '12 at 16:19
  • why do you think pypm is more powerful and reliable than easy_install? any points to support that? any articles / blogposts? – Sandeep Raju Prabhakar Apr 29 '13 at 10:25
  • 2
    I think this would be the article / blog post to support that :P I mean, come on, you support such a comment with typically technical arguments, not with someone else who claims the same, but also may not have arguments. However, arguments would be nice ;) – Herbert Dec 02 '13 at 11:47
3

On OSX these commands worked for me

brew install mysql-connector-c 
pip install MySQL-python
Joe Inner
  • 1,450
  • 1
  • 10
  • 14
3

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.

Om Sao
  • 7,064
  • 2
  • 47
  • 61
  • The original question states clearly the error message, which is "No module named MySQLdb". Your solution gives exactly the same error. Thus, your solution is wrong. – Xxxo Sep 23 '21 at 10:30
  • It connects correctly and doesn't give error. Please check again. – Om Sao Sep 23 '21 at 12:39
  • Please read what the original question is and what I wrote. I never mentioned about connection and the original question is not about connection. Is about ModuleNotFoundError. – Xxxo Sep 24 '21 at 08:00
3

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
nsssayom
  • 364
  • 1
  • 5
  • 21
3

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!
kujiy
  • 5,833
  • 1
  • 29
  • 35
2

I have tried methods above, but still no module named 'MySQLdb', finally, I succeed with

easy_install mysql-python

my env is unbuntu 14.04

Pythoner
  • 5,265
  • 5
  • 33
  • 49
2

If your are using SQLAlchemy and the error is in /site-packages/sqlalchemy/dialects/mysql/mysqldb.py:

from ...connectors.mysqldb import (
                        MySQLDBExecutionContext,
                        MySQLDBCompiler,
                        MySQLDBIdentifierPreparer,
                        MySQLDBConnector
                    )

so you may have missed mysqldb connector for SQLAlchemy and the solution is to re-install sqlalchemy after installing mysql-python module.

mtoloo
  • 1,795
  • 3
  • 22
  • 28
2

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

jfxninja
  • 371
  • 3
  • 11
1

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
ScottK
  • 1,526
  • 1
  • 16
  • 23
1

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

CJT
  • 83
  • 1
  • 10
1

Windows 7 / Python 3.6.5 this works for me:

conda install -y mysqlclient

Tonny
  • 41
  • 2
0

Win10 / Python27 this worked for me:

easy_install mysql-python

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

Cris
  • 2,824
  • 24
  • 23
0

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
wwmwabini
  • 75
  • 6
0

For CentOS 8 and Python3

$ sudo dnf install python3-mysqlclient -y

Paul Bradbury
  • 482
  • 6
  • 8