Monday, April 25, 2011

Talking to mysql from Python on Mac OS X 10.6

image by Sam Pullara

Here's a problem I couldn't solve with Google, although it seems to be a moving target so YMMV.

I wanted to do some work driving a MySQL database with Python. On Windows and Linux I've used MySQLdb so I decided to do the same on Mac.

For reference I got mysql (client and server) through mac ports.

mysql5 --version
mysql5 Ver 14.14 Distrib 5.1.45, for apple-darwin10.4.0 (i386) using readline 6.1

and Python is the stock version:

Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin

First download from here and extract the file somewhere...

mkdir ~/pythondb
cd ~/pythondb
tar -vxf ~/Downloads/MySQL-python-1.2.3.tar.gz

Then you need open up the site.cfg file and make a change as below. Your mysql_config5 maybe in a different spot. You can find out with the command 'which mysql_config5'.

# The path to mysql_config.
# Only use this if mysql_config is not on your PATH, or you have some weird
# setup that requires it.
mysql_config = /opt/local/bin/mysql_config5
Now execute the following:

python build
python install

If everything works you can now import MySQLdb in your python program and start interacting with mysql.

No comments: