Configuring MySQL-Python on OSX Leopard

At WebSight Design we try to remain technology-agnostic. (The right tool or language for the job.) Having said that, we are primarily a PHP shop. However, with the launch of the Google App Engine, we’ve been looking seriously at Python. In recent years, I have become a big fan of development frameworks so to aide me in learning the ways of the snake, I have been using the Django framework.

Getting it all setup wasn’t difficult. The Django installation is very easy. I started to get errors when I was trying to get my models working when I ran

manage.py syncdb

. Come to find out that I needed the MySQL-Python library installed to work correctly on my OSX Leopard machine. Most everything that I illustrate below is to be executed from the command-line. So if you are not familiar with working in the Terminal app, this post is gonna suck for you. So here is what I did to get it working:

First, download MySQL for Python
http://sourceforge.net/projects/mysql-python
for me it was MySQL-python-1.2.2.tar

Unpack it:

tar -xzf MySQL-python-1.2.2.tar

Change to the directory that is created:

cd MySQL-python-1.2.2

Next, locate your “mysql_conf” file.

locate mysql_conf

Copy the location of this file so you can use it in the next few steps.

Next, you need to edit the “site.cfg” file and add the path to your “mysql_conf” file.

mysql_config = "YOUR_PATH_GOES_HERE"

Also, (very important), change the line that reads:

threadsafe = True

TO

threadsafe = False

Save and exit the file.

Now, compile and install the library:

python setup.py build
sudo python setup.py install

NOTE: You may need to run “python setup.py clean” if you previously tried to build and install the library.

Next is the *really* important part. After I kept getting errors trying to run the Python server I found the missing piece on the ProjectMouse.org site.

The key is, you need to create a symbolic link in the location that Python is expecting to find MySQL. This should be a link to the MySQL library that your computer is setup to use.
See here:

sudo mkdir /usr/local/mysql/lib/mysql/
sudo ln -s /usr/local/mysql-5.0.41-osx10.4-i686/lib/libmysqlclient_r.15.dylib /usr/local/mysql/lib/mysql/libmysqlclient_r.15.dylib

NOTE: “mysql-5.0.41-osx10.4-i686″ is where MySQL is installed on my machine. Change this to the location specific to your computer.

Finally, import MySQLdb into Python and test your configuration:

python
import MySQLdb

NOTE: You should not see any errors or warnings. Hit “Ctrl+d” to exit the interepter.

Now go back and run

manage.py syncdb

again and you should not get any errors.

Tags: , , , , ,

Comments are closed.