2

I'm going to connect to mysql database using python. My python version is 2.7.12 and I've installed MySQL using sudo yum install MySQL-python

Package MySQL-python26-1.2.3-11.14.amzn1.x86_64 already installed and  latest version
Nothing to do

Linux version:

$ cat /etc/*-release
NAME="Amazon Linux AMI"
VERSION="2016.09"
ID="amzn"
ID_LIKE="rhel fedora"
VERSION_ID="2016.09"

But when I try to import MySQLdb package it gives an error:

$python
>>> import MySQLdb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named MySQLdb

I've tried different ways as long as I found on the internet but it didn't work.

Any help would be appreciated.

Updated: Required dependency library:

sudo yum install mysql-devel
Matrix
  • 2,399
  • 5
  • 28
  • 53
  • 1
    try: `pip install MySQL-python` – Shubham Namdeo Feb 14 '17 at 08:41
  • wow I tried this command several times already and it didn't work but it worked now! Thank you. Before that I installed python-dev as well. – Matrix Feb 14 '17 at 09:01
  • Wait let me put it as answer so you can mark it as correct. – Shubham Namdeo Feb 14 '17 at 09:05
  • 1
    i've seen this happening a lot to people that use anaconda (if so, my advice is to avoid using that mess). this can happen if system uses one python interpreter with its own packages and anaconda does it's own thing...try running `which python` to see which python interpreter you are running. if it is anything else than your system python (probably /usr/bin/python) or than your pip (should be something like /usr/bin/pip), then your python will not find the packages installed by yum. – grepe Feb 14 '17 at 09:07

2 Answers2

2

Try running:

pip install MySQL-python

Make sure you have depending libraries installed. For that you may run:

sudo dnf install python python-devel mysql-devel redhat-rpm-config gcc

As in your case sudo yum install mysql-devel did the job for you.

Shubham Namdeo
  • 1,845
  • 2
  • 24
  • 40
  • 1
    The dependency library that was needed to run was: sudo yum install mysql-devel then I could install MySQL-python using pip install MySQL-python – Matrix Feb 14 '17 at 15:17
2

install pip and upgrade to the latest version.

apt-get install python-pip
pip install -U pip

Next, install the required development packages.

apt-get install python-dev libmysqlclient-dev

Then...

pip install MySQL-python

This should complete installation. After running these commands successfully, try importing MySQLdb in python interpreter.

>>> import MySQLdb
Venkatesh_CTA
  • 120
  • 10