MySQLdb and Python ImportError

mysqldb python 3
python-mysqldb
modulenotfounderror: no module named 'mysqldb'
mysql-connector-python
pip install mysqldb
install mysqldb python3 ubuntu
python mysql library
python mysql module

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.

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.

No module named MySQLdb, You need to use one of the following commands. Which one depends on what OS and software you have and use. easy_install mysql-python  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.

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

mysql - Import error :No module named MYSQLdb, You have to install the module with: sudo apt-get install python-mysqldb. Reference: This thread on SO. I am trying to install MySQLdb package. I found the source code here.. I did the following: gunzip MySQL-python-1.2.3c1.tar.gz tar xvf MySQL-python-1.2.3c1.tar cd MySQL-python-1.2.3c1 python setup.py build

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.

How to connect MySQLdb in Python 3?, You have to install the MySQLdb package: sudo apt-get update. then sudo apt-​get install python3-dev libmysqlclient-dev. This took a while and finally: sudo pip3​  I have installed mysql server using $ sudo apt-get install mysql-server then I installed python-mysqldb using $ sudo apt-get install python-mysqldb but when I import MySQLdb in python it shows

do this and try again

pip uninstall mysqlclient

No module named MySQLdb, To run MySQL with Python you need to use one of the following commands. And one important node here, there is no MySQLdb for python 3.x. return __import__('MySQLdb') ImportError: No module named MySQLdb and my resolution : pip install MySQL-python yum install mysql-devel.x86_64 at the very beginning, i just installed MySQL-python, but the issue still existed. So i think if this issue happened, you should also take mysql-devel into consideration. Hope this helps.

Python 3 - MySQL Database Access, Python 3 - MySQL Database Access - The Python standard for database "test.​py", line 3, in <module> Import pymysql ImportError: No module named pymysql. MySQLdb is an api for accessing MySQL database using python. It is built on top of MySQL C API. MySQLdb don't yet have support for python 3, it supports only python 2.4 - 2.7. As a result you need to use python 2 for this tutorial.

Python - Connecting to MySQL Databases, Let's take a look at the following Python module ( connect.py ). import mysql.​connector from mysql.connector import Error def connect(): """ Connect to MySQL​  There is no MySQLdb for python3.x. PyMySQL is an interface for connecting to a MySQL database server from Python. It implements the Python Database API v2.0 and contains a pure-Python MySQL client library.

MySQLdb Frequently Asked Questions, ImportError. ImportError: No module named _mysql. If you see this, it's likely you did some wrong when installing MySQLdb; re-read  MySQLdb is a thin Python wrapper around _mysql which makes it compatible with the Python DB API interface (version 2). In reality, a fair amount of the code which implements the API is in _mysql for the sake of efficiency.

Comments
  • Anyway, are you running Apple's pre-installed Python? A Homebrew Python? A different Python, like a python.org binary installer? What do which python and which pip from the shell show? What about import sys; print sys.executable, sys.path inside Python?
  • Also, _mysql is not part of MySQL, it's part of MySQLdb. Somehow you've got part of MySQLdb 1.2.2.0 and part of 1.2.5.1. My guess is that either (a) you've got 1.2.2.0 in your system site-packages, and then you installed 1.2.5.1 into your virtual environment but it only got partially installed, or (b) you've somehow got two different Python 2.7 installations sharing part of their site-packages, and the first pip and first python on your PATH are from different Python installations.
  • There are no errors at all in those logs. There are a bunch of warnings, which are potential 64-bit bugs that the MySQLdb should fix or work around at some point, but they're not relevant here. You can see at the end that it was able to link _mysql.so with no problems, and then it said "Successfully installed MySQL-python".
  • Meanwhile, you still haven't answered my most important question: Are you running the pre-installed Apple Python, a Homebrew Python, or something else? which python from outside of the virtualenv will tell you that, but not from inside it.
  • OK, which OS X version? Starting in (I think) 10.8, Apple changed the way they distributed built-in extra batteries, and sometimes trying to update them doesn't work right. I know they don't distribute MySQLdb in 10.9 (because I'm on a 10.9 machine right now), but I don't know about 10.8 or 10.10 off the top of my head… I know this probably isn't your issue, but it would be nice to rule it out.
  • How did you rebuild the python path?
  • What if I want to use a newer mysql-python? but _mysql is version (1, 2, 5, 'final', 1), what is the corresponding package for _mysql? Is is possible to update / upgrade that _mysql?
  • The above was tested on Python 2.7