No Module Named Mysqldb

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.

4

32 Answers

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)

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:

6

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

10

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

2

Ubuntu:

sudo apt-get install python-mysqldb
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

2

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
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
2
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+

3

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

2

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
1

Try this.

pip install MySQL-python
1

For Python 3.6+

sudo apt-get install libmysqlclient-dev
pip3 install mysqlclient

does the trick

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

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()
pip install --user mysqlclient 

above works for me like charm for me.I go the error from sqlalchemy actually. Environment information :

Marcus Vance
Author

Marcus Vance

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.