I have a problem so a connection a python to database local of MySQLdb.

Message of error is:

import MYSQL    
ImportError:No module named MYSQLdb.

This is a script in python 3.4 to connection my database local MYSQL.

import MYSQLdb
conn=MYSQLdb.connect (host ="localhost", user="root" , passwd="abcd",db="nomi")
name=input("Write your name :   ")

x="insert into user (name) values ('%s')"%(name)

PS: Can you help me please?


sudo apt install libmysqlclient-dev then

  • for python 2.x:

    pip install mysql-python

  • for python 3.x:

    pip install mysqlclient or pip3 install mysqlclient

| improve this answer | |
  • How does this answer the question? – guntbert Jun 20 '18 at 19:06
  • @user1721265: It looks like this answer tries to instruct readers how install the (apparently) missing MYSQL module for Python. – David Foerster Jun 20 '18 at 20:17
  • It looks like OP is only looking for a solution for Python 3 though. – David Foerster Jun 20 '18 at 20:18
  • it is obvious that after seeing "No module MYSQLdb" user have tried to install it, but if it still showing the Import Error msg try to install 'libmysqlclient-dev' first. – technodivesh Jun 22 '18 at 6:18

You have to install the module with:

sudo apt-get install python-mysqldb

Reference: This thread on SO

| improve this answer | |
  • yes , it is installed. – Linus Strike Feb 9 '15 at 12:27
  • Yes , it was installed a module. – Linus Strike Feb 9 '15 at 12:34
  • Thanks a lot, it solved my issue – Syed Aqeel Mar 29 '19 at 5:46

Unfortunately MySQLdb does not support Python 3.

You basically have two options:

  1. Run your script using python2.7, that way you won't need to change the MySQL module. The downside is that you'll probably have to convert some code to python2.x.

  2. Look for python3.x supported modules as explained here: Python 3.4.0 with MySQL database.

| improve this answer | |

I got this issue solved by the following line

pip install pymysql

Also updated the MySql configuration line to

app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://.............'
| improve this answer | |

As far as I know, MySQLdb does not support Python 3. From the page '[MySQL-python]'1 updated on 2013-06-08:

* Python versions 2.4-2.7; Python 3 support coming soon.

mysqlclient-python (https://github.com/PyMySQL/mysqlclient-python) claims support for python 3 but I have no experience with that product. Personally, I use pyodbc (also requires libmyodbc) which looks very similar from a programming point of view.

| improve this answer | |

The answer by @technodivesh worked for me in debian 9 and python 3.5. But with a little modification.

Instead of

sudo apt install libmysqlclient-dev

I used:

sudo apt install default-libmysqlclient-dev

It was suggested here

The first command returned me-

E: Package 'libmysqlclient-dev' has no installation candidate

[I would have added this as a comment to his answer. But, I do not have the required reputation to make comments.]

| improve this answer | |

For Python3 its

sudo apt-get install python-mysqldb

Other commands I ran that may or may not be required (the command above ultimately solved everything for me)

#this was for a new machine set up. one or two of the below failed
sudo apt install python3-pip
pip3 install mysqlclient
pip3 install mysql-connector
pip3 install mysql-connector-python
| improve this answer | |

This is very easy. You have to write just like this:

import Mysql.connector

mydb = Mysql.connector.connect(

mycursor = mydb.cursor()

mycursor.execute("CREATE DATABASE mydatabase")

This code will run correctly without showing any module error

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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