Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQLdb and Python ImportError

I cannot for the life of me figure this one out. I've been searching around the web all day and all the resources seem terribly out dated. From what I can tell getting MySQLdb and Python to play nice together is fairly difficult. I've gotten about as far as I can on this, and I'm not sure how to proceed going forward.

First off, I am running Python 2.7

The error I get when I try and run "import MySQLdb" in the live interpreter is this:

ImportError: this is MySQLdb version (1, 2, 2, 'final', 0), but _mysql is version (1, 2, 5, 'final', 1)

I also see the following error logs when I run "pip install mysql-python" however I'm not sure if thats jsut related to the version mis-match. http://pastebin.com/hqVv6aPZ

I have a python project that has a dependency on MySQLdb and I've been trying to get the virtualenv that I'm running Python from to install the package properly. This is what I've done:

  • I've built MySQL from the source to ensure that I have a 64bit compatible version of MySQL on my machine. I used the --universal flag to ensure this.
  • I have verified that I am running a 64bit version of Python as well.
  • MySQL came from Homebrew
  • mysql-python came from pip

I can't for the life of me figure out what to do here. It seems like there is just a version mis-match between MySQLdb and _mysql on my machine. Is this the case? If so is the solution simply reinstalling an older version of MySQL? It appears that when I force pip to install version 1.2.5 of mysql-python it installs version 1.2.2 of MySQLdb, so i'm lost as to what to do here because I'm not sure what package from homebrew actually correlates to version 1.2.5 for _mysql.

EDIT -

sys.path

'/Users/XXXXXXXXXXX/virtualenvs/qa-automated-tests/lib/python27.zip',
'/Users/XXXXXXXXXXX/virtualenvs/qa-automated-tests/lib/python2.7',
'/Users/XXXXXXXXXXX/virtualenvs/qa-automated-tests/lib/python2.7/plat-darwin', 
'/Users/XXXXXXXXXXX/virtualenvs/qa-automated-tests/lib/python2.7/plat-mac', 
'/Users/XXXXXXXXXXX/virtualenvs/qa-automated-tests/lib/python2.7/plat-mac/lib-scriptpackages', '/Users/XXXXXXXXXXX/virtualenvs/qa-automated-tests/Extras/lib/python',
'/Users/XXXXXXXXXXX/virtualenvs/qa-automated-tests/lib/python2.7/lib-tk', 
'/Users/XXXXXXXXXXX/virtualenvs/qa-automated-tests/lib/python2.7/lib-old', 
'/Users/XXXXXXXXXXX/virtualenvs/qa-automated-tests/lib/python2.7/lib-dynload', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', 
'/Users/adam.stark/virtualenvs/qa-automated-tests/lib/python2.7/site-packages'

Python --version says i'm on version 2.7.5. I've also set the pastebin to public. I'm just not sure what exactly is pertinent information within that dump, it spits back 16 errors.

like image 773
Xenology Avatar asked Nov 22 '14 01:11

Xenology


2 Answers

The issue here ended up being a few things. As abarnert pointed out in the comments of the question, there was a mixing of the system python and the virtualenv python. To resolve this I had to change the project properties of the PyDev project to only point to the virtualenv python instance, then in the PyDev interpreter preferences I had to rebuild the PYTHONPATH.

After this was done, in the virtualenv I had to run the following code:

pip uninstall mysql-python 
pip install mysql-python==1.2.5

This resolved all of the issues.

like image 144
Xenology Avatar answered Oct 24 '22 02:10

Xenology


You can try this. Open terminal and type:

sudo apt-get remove python-mysqldb
sudo apt-get install python-dev libmysqlclient-dev
git clone https://github.com/farcepest/MySQLdb1.git
python setup.py build
sudo python setup.py install

It worked for me. I use Python 2

like image 21
Huy Nguyen Avatar answered Oct 24 '22 03:10

Huy Nguyen