Modulenotfounderror: no module named mysqldb

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

35.6k24 gold badges188 silver badges257 bronze badges

asked Jan 18, 2009 at 9:13

Modulenotfounderror: no module named mysqldb

5

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

9188 silver badges11 bronze badges

answered May 3, 2011 at 17:23

Modulenotfounderror: no module named mysqldb

derevoderevo

8,7682 gold badges21 silver badges19 bronze badges

11

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

answered Aug 24, 2014 at 20:11

Janek OlszakJanek Olszak

3,8691 gold badge27 silver badges22 bronze badges

6

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

answered Jul 11, 2016 at 15:31

gokselgoksel

4,1443 gold badges39 silver badges50 bronze badges

10

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

Kenan Banks

201k34 gold badges151 silver badges171 bronze badges

answered Jan 18, 2009 at 9:25

Evan FosmarkEvan Fosmark

95.8k34 gold badges104 silver badges117 bronze badges

2

Ubuntu:

sudo apt-get install python-mysqldb

om-nom-nom

61.9k13 gold badges181 silver badges225 bronze badges

answered May 14, 2011 at 2:17

panckreouspanckreous

5296 silver badges4 bronze badges

0

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

answered Mar 23, 2016 at 16:08

Modulenotfounderror: no module named mysqldb

A.RaoufA.Raouf

2,0211 gold badge24 silver badges33 bronze badges

3

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

answered Mar 21, 2020 at 13:13

1

for Windows :

pip install mysqlclient pymysql

then:

import pymysql
pymysql.install_as_MySQLdb()

for python 3 Ubuntu

sudo apt-get install -y python3-mysqldb

Modulenotfounderror: no module named mysqldb

cssyphus

35.8k18 gold badges90 silver badges107 bronze badges

answered Aug 4, 2018 at 6:42

Modulenotfounderror: no module named mysqldb

2

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

answered Jan 18, 2021 at 20:18

3

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+

answered Feb 16, 2017 at 21:42

alphiiialphiii

1,4872 gold badges21 silver badges27 bronze badges

3

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

answered Dec 25, 2017 at 14:30

KostyantynKostyantyn

4,8533 gold badges31 silver badges30 bronze badges

1

Try this.

pip install MySQL-python

answered Jun 28, 2015 at 21:01

Modulenotfounderror: no module named mysqldb

Venkat KotraVenkat Kotra

9,9933 gold badges47 silver badges51 bronze badges

1

For Python 3.6+

sudo apt-get install libmysqlclient-dev
pip3 install mysqlclient

does the trick

answered Oct 29, 2019 at 6:41

Umair AyubUmair Ayub

16.9k14 gold badges66 silver badges143 bronze badges

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

answered Jun 20, 2021 at 13:26

Modulenotfounderror: no module named mysqldb

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

answered Dec 3, 2016 at 8:40

Modulenotfounderror: no module named mysqldb

Alfred HuangAlfred Huang

16.9k32 gold badges115 silver badges185 bronze badges

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

answered May 22, 2019 at 20:53

AsrafulAsraful

1,20116 silver badges30 bronze badges

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

answered Oct 5, 2019 at 7:38

Justin LeeJustin Lee

7601 gold badge10 silver badges21 bronze badges

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

answered Jan 18, 2009 at 12:18

Daniel LopezDaniel Lopez

3,2622 gold badges29 silver badges29 bronze badges

2

  • Go to your project directory with cd.
  • source/bin/activate (activate your env. if not previously).
  • Run the command easy_install MySQL-python

Modulenotfounderror: no module named mysqldb

fedorqui

259k99 gold badges524 silver badges577 bronze badges

answered Feb 2, 2015 at 13:36

Modulenotfounderror: no module named mysqldb

GrvTyagiGrvTyagi

4,04333 silver badges37 bronze badges

1

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.

answered Oct 4, 2012 at 11:38

Afshin MehrabaniAfshin Mehrabani

31.4k27 gold badges130 silver badges198 bronze badges

4

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:

>  0x000002338A4C6B00>

This means, database is correctly connected.

answered May 15, 2019 at 15:06

Modulenotfounderror: no module named mysqldb

Om SaoOm Sao

6,2602 gold badges36 silver badges56 bronze badges

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

answered Feb 21, 2020 at 21:27

Modulenotfounderror: no module named mysqldb

nsssayomnsssayom

3381 gold badge3 silver badges20 bronze badges

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!

answered Apr 19, 2021 at 10:01

kujiykujiy

5,3711 gold badge27 silver badges34 bronze badges

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

answered Apr 18, 2016 at 14:57

PythonerPythoner

4,8555 gold badges31 silver badges49 bronze badges

1

On OSX these commands worked for me

brew install mysql-connector-c 
pip install MySQL-python

answered Jan 6, 2017 at 18:59

Modulenotfounderror: no module named mysqldb

Joe InnerJoe Inner

1,3401 gold badge9 silver badges14 bronze badges

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.

answered Apr 24, 2017 at 6:16

mtoloomtoloo

1,6133 gold badges22 silver badges26 bronze badges

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

answered Nov 12, 2019 at 19:09

jfxninjajfxninja

3533 silver badges11 bronze badges

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

answered Dec 18, 2019 at 0:07

Modulenotfounderror: no module named mysqldb

ScottKScottK

1,4661 gold badge15 silver badges23 bronze badges

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

answered Apr 21, 2021 at 13:47

CJTCJT

851 silver badge10 bronze badges

Win10 / Python27 this worked for me:

easy_install mysql-python

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

answered Jul 10, 2019 at 17:16

Modulenotfounderror: no module named mysqldb

CrisCris

2,57122 silver badges21 bronze badges

How do I install MySQLdb?

MySQLdb is an interface for connecting to a MySQL database server from Python..
Step 1: Check Python version. Check if Python is installed or not on your system. ... .
Step 2: Updates information and their dependencies. ... .
Step 3: Install the module..

What is MySQLdb in Python?

MySQLdb is an interface for connecting to a MySQL database server from Python. It implements the Python Database API v2. 0 and is built on top of the MySQL C API. Packages to Install. mysql-connector-python mysql-python.

Does MySQLdb work with python3?

MySQLdb module, a popular interface with MySQL is not compatible with Python 3.

How do I download and install MySQLdb module for Python on Windows?

Download and Install MySQL Connector Python on Windows.
download MySQL connector python for windows..
select MySQL connector python platform-independent zip for windows..
begin to download MySQL connector python zip file for windows..
Python MySQL connector python installation completed for windows..