0
votes

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
2
try with apt-get purge python-mysqldb then pip install MySQL-python - itzMEonTV

2 Answers

4
votes

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

0
votes

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