MySQLdb and Python ImportError

2019-05-02 00:44发布

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.

3条回答
虎瘦雄心在
2楼-- · 2019-05-02 01:28

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

查看更多
叛逆
3楼-- · 2019-05-02 01:31

I had the same problem, produced for a different reason tho. I don't have root privileges on my working machine (part of the university infrastructure). I had to install MySQLdb locally by issuing pip install --user MySQL-python (from this guide ). This produced the same ImportError: this is MySQLdb version (1, 2, 3, 'final', 0), but _mysql is version (1, 2, 5, 'final', 1) mismatch. Installing version 1.2.5 didn't work.

What I did : uninstall from local and re-install version 1.2.3 as indicated by the error

pip install --user MySQL-python==1.2.3

Error disappeared.

查看更多
Summer. ? 凉城
4楼-- · 2019-05-02 01:34

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.

查看更多
登录 后发表回答