ImportError: No module named MySQLdb

PythonMysqlFlaskSqlalchemyMysql Python

Python Problem Overview


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.

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

Or try this:

apt-get install python-mysqldb

Solution 3 - Python

may you try

pip install mysqlclient

Solution 4 - Python

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.

Solution 5 - Python

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

Solution 6 - Python

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.

Solution 7 - Python

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.

Solution 8 - Python

In ubuntu 20 , you can try this :

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

Solution 9 - Python

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

Solution 10 - Python

yum install MySQL-python.x86_64

worked for me.

Solution 11 - Python

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

Solution 12 - Python

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

xcode-select --install

Solution 13 - Python

sudo apt-get install python-mysqldb

Solution 14 - Python

I just spent a long 9 hours debugging this issue and then finally fixed it. 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")

I request everyone to upvote this answer, because I dont want anyone else to waste their time like I did.

Solution 15 - Python

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

happy coding!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3182194View Question on Stackoverflow
Solution 1 - PythonDazWorrallView Answer on Stackoverflow
Solution 2 - PythonEdward van KuikView Answer on Stackoverflow
Solution 3 - PythonDoniView Answer on Stackoverflow
Solution 4 - PythonAdventuristView Answer on Stackoverflow
Solution 5 - PythonWaseem Akram MalikView Answer on Stackoverflow
Solution 6 - Pythonuser6140865View Answer on Stackoverflow
Solution 7 - PythonseokhoonleeView Answer on Stackoverflow
Solution 8 - PythonDachuanZhaoView Answer on Stackoverflow
Solution 9 - PythonChen LevyView Answer on Stackoverflow
Solution 10 - PythonhamedView Answer on Stackoverflow
Solution 11 - PythonM E S A B OView Answer on Stackoverflow
Solution 12 - Pythonchandan kharbandaView Answer on Stackoverflow
Solution 13 - Pythonkamran kausarView Answer on Stackoverflow
Solution 14 - PythonpsychoSherlockView Answer on Stackoverflow
Solution 15 - PythonAnteneh Kassaw BizunehView Answer on Stackoverflow