Installing Django for Leopard with MySQL Support

I was following along with the instructions from Chapter 2 of the Django Book, an online reference for learning and using Django a Python web framework when I hit a small snag in adding a database for my project. My initial experience with Mac OS X Leopard (10.5) and adding open-source software has been a pleasure.

I have yet to encounter the dependency nightmare I have experienced with previous Mac OS X systems. Of course, I have also tamed my need for the absolute bleeding edge of software by avoiding nightly builds and the like, at least for time being.

Installing Django

# Download Django <URL: http://www.djangoproject.com/download/ />
dev:www $ tar xzvf Django-*.tar.gz.
dev:www $ cd Django-*.
dev:www $ sudo python setup.py install

# Test the django module
dev:www $ python
>>> import django
>>> django.VERSION

Add MySQL Support to Python

Adding MySQL support for Python presented the only issue. A unexpected configuration error prevents the MySQLdb module from compiling and then a different issue throws a 'file not found' error when the module is loaded into Python.

I am not familiar enough with either system to say which developer has changed their installed path or even if it is some erroneous oversight on my part when originally installing MySQL or MySQLdb. In any case, here is what I need to do to complete the compile and install of MySQLdb for Mac OS X Leopard:

Update:Installing on Mac OS X Server Leopard 10.5

When compiling/installing MySQLdb for Mac OS X Server Leopard 10.5.x one will receive a different error for gcc about not being able to find my_conf.h. Because MySQL 5.0 is pre-installed in the Server addition this error stumped me until I read an Apple Support Article that explains that the client header files (.h) are not included with the Server edition for what I can only imagine is some sort of security preference.

$ curl -O http://www.opensource.apple.com/darwinsource/other/MySQL-43.binaries.tar.gz
# Follow the instructions to add MySQL header files.

Mac OS X (Client) Leopard does not come with MySQL pre-installed and the standard binary package for installing from MySQL includes the client and server files needed to interact with the service as well as the bits needed for MySQLdb. A small note that SQLite is supported by default as of Python 2.5+ and is the staple for use in many Cocoa applications for Mac OS X. For local application support or general 'tinkering' its pretty handy database, just not as fast as MySQL when it comes to dynamically serving content to web clients.


# Download the latest stable version of MySQLdb
# <URL: http://mysql-python.sourceforge.net/ />
$ tar xzf MySQL-python-*.tar.gz
$ cd MySQL-python-*
$ find / -name mysql_conf

# Edit site.cgf at line 13 to add your full mysql_conf path
mysql_config = /usr/local/mysql/bin/mysql_config

# <URL: http://forums.mysql.com/read.php?50,175059,179979#msg-179979 />
# Edit these lines from _mysql.c

 37	//ifndef uint 
 38	//define uint unsigned int 
 39	//endif 

484	//uint port = MYSQL_PORT; CHANGED to next line
485	unsigned int port = MYSQL_PORT;
486	//uint client_flag = 0; CHANGED to next line
487	unsigned int client_flag = 0;

# Issue 1 is now solved and you can compile MySQLdb
$ python setup.py build 
$ sudo python setup.py install 

# Issue 2 is solved by creating a symbolic link to libmysqlclient_r.15.dylib library.
$ sudo mkdir /usr/local/mysql/lib/mysql 
$ sudo ln -s /usr/local/mysql/lib/libmysqlclient_r.15.dylib\
	/usr/local/mysql/lib/mysql/libmysqlclient_r.15.dylib

# test MySQLdb module in python
$ python
>>> import MySQLdb

# no trace back errors == 'sweetness'

For future reference

System paths for Mac OS X Leopard (10.5) default pre-installed Python framework.

$ python
>>> import sys
>>> print sys.path
	''
	'/Library/Python/2.5/site-packages/MySQL_python-1.2.2-py2.5-macosx-10.5-ppc.egg'
	'/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python25.zip'
	'/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5'
	'/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-darwin'
	'/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac',
	'/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/\
		plat-mac/lib-scriptpackages',
	'/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python',
	'/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-tk',
	'/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-dynload',
	'/Library/Python/2.5/site-packages',
	'/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/PyObjC'

Start a New Project

$ mkdir Django
$ cd Django/
$ django-admin.py startproject LearningDjango
$ ls -1 LearningDjango/
total 24
__init__.py		# Python sees this directory as a package
manage.py		# command-line utility
settings.py		# Django project settings and configuration
urls.py			# "Table of Contents" for the project

Start the Development Server

$ python manage.py runserver
Validating models...
0 errors found.

Django version 0.96.1, using settings 'LearningDjango.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[08/Jan/2008 20:12:10] "GET / HTTP/1.1" 404 2069
About RSS Feed Search