Daily Archives: December 7, 2008

MySQL with Python on Mac

This entry will be more technical than most, keying of the “open” and “research” more than “education”.

In an analysis I’m working on, I’m going to be munging the data all sorts of ways and the graphical statistics environment I’ve been using (JMP) is going to be painful for me to use. Particularly because it updates state and it’s hard to remember how I got there. I’ve had great success on a project recently using GNU Make to process the data from raw to beautiful and I want to try that here.

In my other project, the raw data is in XML so I got handy with Python and an XML processor. In this project, my raw data is in a MySQL database. I thought that would be as straightforward, but it hasn’t been. That may be due more to using a Mac this time around though (as opposed to Windows and Linux previously).

In any event, here is what I had to do to get the MySQL-python on my MacBook Pro. I hope this saves someone else the trouble. In the end, I made an .egg, so if you want to skip the rest you can just download that: MySQL_python-1.2.2-py2.5-macosx-10.5-i386.egg

First I had to get setuptools working. I noticed had two versions of Python installed, Leopard’s and MacPorts’, and decided to remove the MacPorts one to keep things clean. (Incidentally, I regret ever making use of MacPorts. I’ve much rather have built binaries ala Fink.) Once I had setuptools working I thought I could install the MySQL driver with simply,

easy_install MySQL-python

Unfortunately, that fails with a compile error,

In file included from /usr/local/mysql/include/mysql.h:47,                 from _mysql.c:40:
/usr/include/sys/types.h:92: error: duplicate ‘unsigned’
/usr/include/sys/types.h:92: error: two or more data types in declaration specifiers
error: Setup script exited with error: command 'gcc' failed with exit status 1

Fortunately I found a blog that provided a workaround,
Install MySQL server from mysql.com
Download and unpack the MySQLdb package (download URL here)
Fix the path to mysql_config, either by editing setup-posix.py (editing mysql_config.path value) or adding /usr/local/mysql/bin/ to your PATH. (i.e. export PATH=$PATH:/usr/local/mysql/bin/)
In the folder run python setup.py clean and python setup.py build

At this point you’ll probably get this error,

/usr/include/sys/types.h:92: error: duplicate ‘unsigned’/usr/include/sys/types.h:92: error: two or more data types in declaration specifiers

To get around this, edit _mysql.c to change

#define uint unsigned int

#define /* uint unsigned int */
Then build again. You’ll probably see this warning,

ld: warning in build/temp.macosx-10.5-i386-2.5/_mysql.o, file is not of required architecture
ld: warning in /usr/local/mysql/lib/libmysqlclient_r.dylib, file is not of required architecture
ld: warning in /usr/local/mysql/lib/libmygcc.a, file is not of required architecture

I ignored them and haven’t encountered any problems. I’m on a 32-bit Intel Mac though. If you’re on 64-bit, this blog post may be able to help.

Run sudo python setup.py install to install.

Then I ran this code to test. Once I knew it was working, I tried to find a way to share the binary. I was able to build a binary .egg with python setup.py bdist_egg, which made MySQL_python-1.2.2-py2.5-macosx-10.5-i386.egg

I’d like to upload it to the PyPi record, having registered and set up my .pypirc file, but I don’t have permission. After writing this, I will contact to package index owner, Andy Dustman, and see if he can include it. If he does, then the simple command I was expecting at first should work for 32-bit Mac users now on.

easy_install MySQL-python

And until then, this negligibly longer command should work,

easy_install http://www.cs.cmu.edu/%7Etaleahma/projects/misc/MySQL_python-1.2.2-py2.5-macosx-10.5-i386.egg