171

I am referring the following tutorial to make a login page for my web application. http://code.tutsplus.com/tutorials/intro-to-flask-signing-in-and-out--net-29982

I am having issue with the database. I am getting an

ImportError: No module named MySQLdb

when I execute

http://127.0.0.1:5000/testdb

I have tried all possible ways to install python mysql, the one mentioned in the tutorial, easy_install, sudo apt-get install.

I have installed mysql in my virtual env. My directory structure is just the same as whats explained in the tutorial. The module is sucessfully installed in my system and still I am getting this error.

Please help. What could be causing this.

3
  • You've installed MySQL but have you installed the MySQLdb package for Python?
    – Ffisegydd
    Mar 7, 2014 at 14:13
  • I used sudo apt-get install mysql-python. When i used pip i got Environment error for mysql_config Mar 7, 2014 at 14:19
  • mysql is installed within my virtual enviroment and working properly and mysqldb has also been installed. Mar 7, 2014 at 14:28

16 Answers 16

382

If you're having issues compiling the binary extension, or on a platform where you cant, you can try using the pure python PyMySQL bindings.

Simply pip install pymysql and switch your SQLAlchemy URI to start like this:

SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://.....'

There are some other drivers you could also try.

10
  • Did I miss something? Why isn't this listed here? Specifically that dialect name: docs.sqlalchemy.org/en/latest/core/engines.html. This was exactly what I had wrong though, thanks.
    – CashIsClay
    Apr 6, 2018 at 20:13
  • Note the db uri is somewhat dependent upon the connector you use :) Jun 13, 2018 at 7:50
  • this didn't work with python2.7.* for me. use of pip install MySQL-python worked. Nov 29, 2018 at 7:43
  • 2
    this one helped so much while searching after a solution for flask-sqlalchemy errors. I faced: 1. Error running WSGI application ModuleNotFoundError: No module named 'MySQLdb' 2. (_mysql_exceptions.OperationalError) (2006, 'SSL connection error: SSL_CTX_set_tmp_dh failed') (Background on this error at: sqlalche.me/e/e3q8) thank you very much and I hope, that others will find this solution with this error descriptions faster as I have done!
    – bky
    Mar 15, 2019 at 23:01
  • Worked on CentOS, seems to be the most non-OS-dependent answer. Thanks!
    – trpt4him
    Mar 21, 2019 at 14:30
103

Or try this:

apt-get install python-mysqldb
5
  • 10
    I think the answer to the question is this Sep 16, 2014 at 12:47
  • 2
    python-mysqldb has some dependencies like VC++ library and sometimes doesn't work on 64 bit OS Oct 27, 2015 at 16:52
  • 1
    This worked for me too. Ubuntu 16.04 with python 2.7
    – genepool99
    Jun 10, 2016 at 17:16
  • For those people who this answer didn't resolve their issue this might be due to a virtualenv issue see: stackoverflow.com/a/43984484
    – Chen Levy
    May 15, 2017 at 16:27
  • More accurately: sudo apt-get install python-mysqldb
    – Tina J
    Dec 2, 2017 at 5:33
40

may you try

pip install mysqlclient
6
  • 2
    Works for me on python 3.7.3
    – nsssayom
    Oct 6, 2019 at 15:29
  • 1
    Thanks for sharing, It worked when I was using Windows but I had some issues with mysqlclient (OSError: mysql_config not found) when I was trying to run the same app on Ubuntu, the accepted answer solved it.
    – Bilal
    Oct 7, 2019 at 20:05
  • Works on OSX with anaconda and python 3.7.6 Apr 5, 2020 at 20:36
  • simple! very simple and clear. May 6, 2022 at 23:09
  • This works on python 3.10 Feb 24 at 14:57
19

My issue is :

return __import__('MySQLdb')
ImportError: No module named MySQLdb

and my resolution :

pip install MySQL-python
yum install mysql-devel.x86_64

at the very beginning, i just installed MySQL-python, but the issue still existed. So i think if this issue happened, you should also take mysql-devel into consideration. Hope this helps.

2
  • 3
    pip install MySQL-python yeah, that's the ticket
    – Kirby
    Jun 29, 2016 at 23:21
  • This depends on Microsoft Visual C++ libraries in Windows environments
    – Erizo
    Feb 10, 2020 at 20:50
16

I got this issue when I was working on SQLAlchemy. The default dialect used by SQLAlchemy for MySQL is mysql+mysqldb.

engine = create_engine('mysql+mysqldb://scott:tiger@localhost/foo')

I got the "No module named MySQLdb" error when the above command was executed. To fix it I installed the mysql-python module and the issue was fixed.

sudo pip install mysql-python
1
  • 3
    before install mysql-python, you still need to install mysql or something containing the msyql_config command.
    – mxi1
    Feb 13, 2015 at 7:09
10

It depends on Python Version as well in my experience.

If you are using Python 3, @DazWorrall answer worked fine for me.

However, if you are using Python 2, you should

sudo pip install mysql-python

which would install 'MySQLdb' module without having to change the SQLAlchemy URI.

2
10

So I spent about 5 hours trying to figure out how to deal with this issue when trying to run

./manage.py makemigrations

With Ubuntu Server LTS 16.1, a full LAMP stack, Apache2 MySql 5.7 PHP 7 Python 3 and Django 1.10.2 I really struggled to find a good answer to this. In fact, I am still not satisfied, but the ONLY solution that worked for me is this...

sudo apt-get install build-essential python-dev libapache2-mod-wsgi-py3 libmysqlclient-dev

followed by (from inside the virtual environment)

pip install mysqlclient

I really dislike having to use dev installs when I am trying to set up a new web server, but unfortunately this configuration was the only mostly comfortable path I could take.

1
  • If you don't have '-dev' packages and a compiler, you cannot install any Python module which has a C extension component that needs to be compiled. Your only option would be to use a separate build box which has the extra bits and build Python wheel files. Then copy the wheels to your target system and install the packages from wheels instead. Oct 8, 2016 at 21:40
6

In ubuntu 20 , you can try this :

sudo apt-get install libmysqlclient-dev
sudo apt-get install gcc
pip install mysqlclient
0
3

While @Edward van Kuik's answer is correct, it doesn't take into account an issue with virtualenv v1.7 and above.

In particular installing python-mysqldb via apt on Ubuntu put it under /usr/lib/pythonX.Y/dist-packages, but this path isn't included by default in the virtualenv's sys.path.

So to resolve this, you should create your virtualenv with system packages by running something like:

virtualenv --system-site-packages .venv

2
yum install MySQL-python.x86_64

worked for me.

2

for Ubuntu 20.04 with python3

 sudo apt-get install python3-mysqldb

by default, this work for me

Create a sqlite engine instance

engine = create_engine('mysql://username:password@your_host/your_dbname')

OR

pip install pymysql

Create a sqlite engine instance

create_engine('mysql://...

2

I have the same issue the install mySQL connector and resolved

pip install mysqlclient

more details -> SQLAlchemy official docs

1

Here's what I did:

First, install a python package: pip3 install PyMySQL

Then add the following lines of code to your Project:

import pymysql
pymysql.install_as_MySQLdb()

this should fix your issue by now, the following one is optional and for anyone who is using SqlAlchemy to connect to mysql server:

Replace engine = create_engine("mysql://YOURMYSQLURL") to: engine = create_engine("mysql+pymysql://YOURMYSQLURL")

2
  • 1
    This wonderfully works for me, while others don't. Thanks!
    – Nam G VU
    Apr 28 at 1:50
  • 1
    @NamGVU ikr!!!.... I tried all the online solutions but it actually turns out the right answer was in the documentation itself.... Even ChatGPT did not recommend this answer. Upvote to help others!! Apr 29 at 6:08
0

Got so many errors related to permissions and what not. You may wanna try this :

xcode-select --install
0

sudo apt-get install python-mysqldb

-1

I have faced this type of error when I tray to build python flask eccomerce web-application

ModuleNotFoundError: No module named 'MySQLdb'?

I tray to see the version of python installed on my window 10

it was python 3.10

so MYSQL-PYTHON is compatable with the other version of python 3.8

I delet python 3.10 and I nstalled the version python 3.8

It become ok!

python --version

3.8

pip --version

python 3.8

so install the connector

pip install MYSQL-PYTHON

successfuly installed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.