MySQL-Python and Apple OSX 10.5 (Leopard)

UPDATE 30/12/09 : Snow Leopard and most recent versions of MySQL-python have broken the recipe below: you'd better check this blog post for an updated version (it worked for me). In a nutshell, the new method involves using the Macports installation if you're on Leopard, or specifying your 64-bit architecture during the compile process if you're on Snow Leopard... but if you're still getting errors, there's nothing else to do than invoking the almighty google for help . .


Hey there - just a quick sunday post (oh god what a nerd I am) to report on my latest experience installing the usual stack for python/django web development on my MacBookPro: MySQL [handy links: install info | download page | do I need the 64 bit mac package?], the python bindings to MySQL (MySQL-Python) and Django. Apache is not needed for now as I'll be using the django builtin server for development (and it should be already installed on your mac). Well anyways, there's lots of posts on how to install the whole thing, I tried a couple of times in the past but always got stuck with a bloody MySQLdb error [EnvironmentError: mysql_config not found]. So I always resorted to the ready-made-stack solutions - which, by the way, work fantastically - I'm talking here about things such as Bitnami's DjangoStack.


However, this time I decided to crack the problem. I installed MySQL, then downloaded the MySQLdb package manually (you can try install it using easy-install, but it wont work cause you gotta edit a couple of things first). Then followed the clear and useful instructions outlined on this blog post by Ken Ingle (another useful post is also this one). Summing them up:

After unpacking the files, in the MySQL-python-1.2.2 directory edit the _mysql.c file and do the following:

1) Remove these lines [34 +]:

ifndef uint

define uint unsigned int


2) Change the following [484 +]

uint port \= MYSQL_PORT; uint client_flag \= 0;

with this:

unsigned int port \= MYSQL_PORT; unsigned int client_flag \= 0;

3) Then in order to avoid the irritating EnvironmentError: mysql_config not found just edit the file and change the mysql_config.path into this [line 27]:

mysql_config.path \= "/usr/local/mysql/bin/mysql_config"

4) Create a symlink:

$ sudo ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql

5) Build and install (and don't let the warnings scare you)

$ sudo python build $ sudo python install

6) Test

>>> import MySQLdb >>> MySQLdb.apilevel '2.0'

7) Enjoy (here's the user-guide and an example) ):

import MySQLdb

conn \= MySQLdb.connect(host\="localhost", user\="root", passwd\="nobodyknow", db\="amit") cursor \= conn.cursor()

stmt \= "SELECT * FROM overflows" cursor.execute(stmt)

# Fetch and output result \= cursor.fetchall() print result

# get the number of rows numrows \= int(cursor.rowcount)

# Close connection conn.close()

Cite this blog post:

Michele Pasin. MySQL-Python and Apple OSX 10.5 (Leopard). Blog post on Published on Aug. 30, 2009.

Comments via Github:

See also: