Xenology Xenology - 1 year ago 106
MySQL Question

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.

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.



'/Users/XXXXXXXXXXX/virtualenvs/qa-automated-tests/lib/python2.7/plat-mac/lib-scriptpackages', '/Users/XXXXXXXXXXX/virtualenvs/qa-automated-tests/Extras/lib/python',

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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download