Installing Django with MySQL on Mac OS X

Django    Mac OS X    MySQL    Python    2008-06-04

Django's official installation instructions are here:

http://www.djangoproject.com/documentation/install/

However, they get a little vague around the section titled "Get your database running".

I followed this post and, aside from a few missing notes, got the Django/MySQL install done in about 15 minutes:

http://antoniocangiano.com/2007/12/22/ how-to-install-django-with-mysql-on-mac-os-x/

Installing Django

I followed the convention this blogger used and just put the Django install in my user directory, under a folder called "Code":

/Users/myusername

mkdir Code

cd Code/

svn co http://code.djangoproject.com/svn/django/trunk django_trunk

To tell Python where to find Django:

cd /Library/Python/2.5/site-packages/

vi django.pth

Paste this single line:

/Users/myusername/Code/django_trunk

And add it to your path:

vi .bash_profile

PATH=$PATH:$HOME/bin:/Users/myusername/Code/django_trunk/django/bin

Installing MySQL

Download MySQL-python-1.2.2.tar.gz to your home directory (/Users/yourusername/) - get it here:

http://sourceforge.net/project/showfiles.php?group_id=22307&package_id=15775&release_id=491012

Untar and switch to the MySQl folder:

tar xvfz MySQL-python-1.2.2.tar.gz

cd MySQL-python-1.2.2

Edit _mysql.c lines 37, 38 and 39 as follows:

//#ifndef uint
//#define uint unsigned int
//#endif

You'll probably need to edit site.cfg and set

threadsafe = False

From MySQL-python-1.2.2/ run:

python setup.py build

sudo python setup.py install

When I tried to run

python setup.py build

I got these errors:

myusername:MySQL-python-1.2.2 myusername$ python setup.py build
sh: mysql_config: command not found
Traceback (most recent call last):
  File "setup.py", line 16, in 
    metadata, options = get_config()
  File "/Users/myusername/MySQL-python-1.2.2/setup_posix.py", line 49, in get_config
    libs = mysql_config("libs")
  File "/Users/myusername/MySQL-python-1.2.2/setup_posix.py", line 24, in mysql_config
    raise EnvironmentError, "%s not found" % mysql_config.path
EnvironmentError: mysql_config not found

I found the answer here (thank you Google!):

http://www.keningle.com/?feed=rss2&p=11

If you met the following error when try to build

mysql_config
    raise EnvironmentError, "%s not found" % mysql_config.path
EnvironmentError: mysql_config not found

Then edit the setup_posix.py file and change the mysql_config.path

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

In my case, I edited the setup_posix.py thusly:

# mysql_config.path = "mysql_config"
mysql_config.path = "/usr/local/mysql-5.0.45-osx10.4-i686/bin/mysql_config"

After the install, don't forget to create the symlink:

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

Verify the installation

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

>>> import django
>>> print django.VERSION
(0, 97, 'pre')

>>> exit()

Verify that django-admin.py is in your path:

django-admin.py

Type 'django-admin.py help' for usage.