0

Im trying to connect mysql through python and have the following packages installed.

apt-get install python-pip

pip install flask

apt-get install python-mysqldb

pip install flask-restful

After searching few of the posts, tried installing the below too: (which doesnt seem to work or is not installing)

apt install libmysqlclient-devel

easy_install mysql-python

pip install MySQL-python

My python version : Python 2.7.12

In my script im using:

import MySQLdb 

And in my command if i try:

python nameserver.py

I get the error as:

import MySQLdb ImportError: No module named MySQLdb 
1
  • try with apt-get purge python-mysqldb then pip install MySQL-python
    – itzMEonTV
    CommentedApr 3, 2017 at 12:37

2 Answers 2

4

You can solve to use this:

first, install build-essential with this:

apt install build-essential

and then, install libmysqlclient-dev with this:

apt install libmysqlclient-dev

then try install mysqlclient with pip like this:

pip install mysqlclient

1
  • in Centos, you can install it "yum install mysql-dev"
    – woheras
    CommentedJun 28, 2017 at 9:27
0

I recommend you to use PyMysql . I found it quite easy to use .

Here is an example :

 import pymysql conn = pymysql.connect(host='localhost', port=3306, user='<username>', passwd='<password>', db='mysql') cur = conn.cursor() cur.execute("SELECT <Column> FROM <Table>") 

Here you can find out more about pyMysql : https://github.com/PyMySQL/PyMySQL

1
  • After changing the new code and commenting the old one, if i run filename.py in cmd, why does it still consider the old code and through the same error, is there any restart mechanism in python..
    – Sach
    CommentedApr 4, 2017 at 5:50

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.