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 Commented Apr 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

Sign up to request clarification or add additional context in comments.

1 Comment

in Centos, you can install it "yum install mysql-dev"
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 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.