169

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.

Ilja Everilä
  • 50,538
  • 7
  • 126
  • 127
user3182194
  • 1,729
  • 2
  • 13
  • 9
  • You've installed MySQL but have you installed the [MySQLdb](https://pypi.python.org/pypi/MySQL-python/1.2.4) package for Python? – Ffisegydd Mar 07 '14 at 14:13
  • I used sudo apt-get install mysql-python. When i used pip i got Environment error for mysql_config – user3182194 Mar 07 '14 at 14:19
  • mysql is installed within my virtual enviroment and working properly and mysqldb has also been installed. – user3182194 Mar 07 '14 at 14:28

16 Answers16

375

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.

DazWorrall
  • 13,682
  • 5
  • 43
  • 37
  • Did I miss something? Why isn't this listed here? Specifically that dialect name: http://docs.sqlalchemy.org/en/latest/core/engines.html. This was exactly what I had wrong though, thanks. – CashIsClay Apr 06 '18 at 20:13
  • Note the db uri is somewhat dependent upon the connector you use :) – Braden Holt Jun 13 '18 at 07:50
  • this didn't work with python2.7.* for me. use of `pip install MySQL-python` worked. – Rahul Goyal Nov 29 '18 at 07: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: http://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 '19 at 23:01
  • Worked on CentOS, seems to be the most non-OS-dependent answer. Thanks! – trpt4him Mar 21 '19 at 14:30
  • be remember to change ` SQLAlchemy URI ` ! this works for python 3 – penny chan Aug 25 '19 at 05:27
  • In my case(Python Anconda 3) , your link which has other connectors worked . I used MySQL-Connector – Pramod Sharma Aug 28 '19 at 04:08
  • Do not use pymysql driver because it's too slow ! ! ! – DachuanZhao May 09 '20 at 09:41
  • It works also for MariaDB (tested for v10.4, via SQLAlchemy). – mirekphd Aug 16 '20 at 18:28
103

Or try this:

apt-get install python-mysqldb
Edward van Kuik
  • 1,357
  • 1
  • 9
  • 9
39

may you try

pip install mysqlclient
Doni
  • 511
  • 5
  • 5
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.

Adventurist
  • 199
  • 1
  • 2
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
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
  • 3
    before install mysql-python, you still need to install mysql or something containing the msyql_config command. – mxi1 Feb 13 '15 at 07: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.

seokhoonlee
  • 1,028
  • 12
  • 18
  • 2
    If anyone is down here, and getting environment error: mysql_config not found kinda thing, then sudo apt-get install libmysqlclient-dev can help you. Original answer: http://stackoverflow.com/a/5178698/2190689 – ssi-anik Nov 11 '16 at 18:11
  • If you get config parser type of error, then you may look at this. http://stackoverflow.com/a/23978968/2190689 – ssi-anik Nov 21 '16 at 11:03
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.

  • 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. – Graham Dumpleton Oct 08 '16 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
DachuanZhao
  • 1,181
  • 3
  • 15
  • 34
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

Community
  • 1
  • 1
Chen Levy
  • 15,438
  • 17
  • 74
  • 92
2
yum install MySQL-python.x86_64

worked for me.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
hamed
  • 21
  • 1
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://...

M E S A B O
  • 783
  • 1
  • 11
  • 15
2

I have the same issue the install mySQL connector and resolved

pip install mysqlclient

more details -> SQLAlchemy official docs

Shakeel Haider
  • 107
  • 1
  • 5
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")

Nimantha
  • 6,405
  • 6
  • 28
  • 69
psychoSherlock
  • 112
  • 1
  • 9
  • 1
    This wonderfully works for me, while others don't. Thanks! – Nam G VU Apr 28 '23 at 01: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!! – psychoSherlock Apr 29 '23 at 06: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

kamran kausar
  • 4,117
  • 1
  • 23
  • 17
-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

Nimantha
  • 6,405
  • 6
  • 28
  • 69