I\'ve been at this for some time and read many sites on the subject. suspect I have junk lying about causing this problem. But where?
This is the error when I impor
It's caused by that your Python is 32bits, but somehow, the installed MySQL library is 64bits. To solve the problem, here you can install it manually with following commands:
wget http://downloads.sourceforge.net/project/mysql-python/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz
tar xvf MySQL-python-1.2.3.tar.gz
cd MySQL-python-1.2.3
ARCHFLAGS="-arch i386" python setup.py install
With ARCHFLAGS="-arch i386", it should be compiled as i386 architecture.
I have a fresh MacBook Air, and I managed to get MySQLdb working by doing the following: (Snow Leopard 10.6.6, preinstalled Python)
uname -a
Darwin Braindamage.local 10.6.0 Darwin Kernel Version 10.6.0: Wed Nov 10 18:13:17 PST 2010; root:xnu-1504.9.26~3/RELEASE_I386 i386
Download the MySQL 32-bit dmg file from mysql pages, Install it.
Add the following lines to your ~/.profile (or ~/.bash_profile):
PATH="/usr/local/mysql/bin:${PATH}"
export PATH
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/
export VERSIONER_PYTHON_PREFER_64_BIT=no
export VERSIONER_PYTHON_PREFER_32_BIT=yes
After saving, type the following in the terminal: source ~/.profile
Download the MySQL-python-1.2.3.tar.gz unzip, untar, cd to that directory
python2.5 setup.py build
sudo python2.5 setup.py install
exit that directory (or you'll get a warning)
python2.5
import MySQLdb
or
python
import MySQLdb
works the way it should!!
I had the same problem with MySQLdb in Mac OS X and Ubuntu, so I switched to the official MysQL Python Connector: (available on PyPI):
sudo pip install mysql-connector-python
or by download from: http://dev.mysql.com/downloads/connector/python/
Documentation: http://dev.mysql.com/doc/refman/5.5/en/connector-python.html
It's easy to use and also compatible with PEP 249 (Python DB API version 2.0).
It appears that this issue is related to the version of mysql that you have on the box. When compiling the python mysql component it uses mysql_config which usually lives in /usr/local/mysql/bin to determine what flags to pass to CC when compiling the mysql component. If you have a 32 bit version of mysql running then you will get a 32 version of this component even if you have the 64 bit version of python. As an example I had the following version of mysql...
/usr/local/mysql-5.5.16-osx10.6-x86 and I should have been running... /usr/local/mysql-5.5.16-osx10.6-x86_64
Changing over to the 64bit version of mysql on my machine and then running... sudo pip install mysql-python
fixed the problem for me. You can run the following command to verify how it is going to build the mysql component...
/usr/local/mysql/bin/mysql_config --cflags
You should see something like this...
-I/usr/local/mysql/include -Os -g -fno-common -fno-strict-aliasing -arch x86_64
This problem was solved for me by selecting the correct python version. If you installed with macports:
sudo port select python python26
This is a shot in the dark - not familiar with MACOSX - but I saw a similar problem under Linux and could only resolve it by:
Next I tried to import MySQLdb and made sure I got a simple error that the package did not exisit - at least I knew MySQLdb was 100% removed
Then I a clean install via the package manager if that option exists as it will be 100% compatible with your platform and libs. Compiling etc. is great but you have to do lots of leg work to make sure that you have the right MySQL client libs etc. to link to (based upon my painful experience)
Good luck.
Worst case ... you could use the alternative PyMySQL pure python option (http://pypi.python.org/pypi/PyMySQL/0.2) but I have to confess most people recommend MySQLdb