How to install Django with MySQL on Mac OS X

Installing Django on Mac OS X Leopard is supposed to be very straightforward, but if you are new to it, you may encounter a few puzzling questions and, in the case of MySQL, even a couple of headaches. I’m writing about this for the benefit of those of you who may attempt and struggle with this feat. MacPorts is not required for this how-to.

First and foremost, we are going to install Django from its svn repository, as opposed to obtaining the 0.96 release archive. The reason for this is that the trunk version implements a few new features. The development code is also rather stable and used by most people in production mode, even for sites like the Washington Post.

Checkout Django

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

Tell Python where Django is

Mac OS X 10.5 already ships with Python 2.5.1, thus you won’t have to install it. You can verify this by running python in the Terminal (use exit() to get out of the python shell). What you need to do is inform Python about the location of your django_trunk directory. To do this create the following file:

/Library/Python/2.5/site-packages/django.pth

Within this file, place only one line containing the path to your django_trunk folder. In my case, this is:

/Users/Antonio/Code/django_trunk

Of course, change it to the full path location of the directory on your filesystem.

Add django-admin.py to your PATH

The bin directory within the django folder (which is inside django_trunk itself) contains several management utilities. We need therefore to add the following to the PATH (again, change it to your own location):

/Users/Antonio/Code/django_trunk/django/bin

How you go about doing this, depends on the shell you are using, and I’m assuming you are able to export a shell variable on your own. In case you are using the bash shell (as I do) then you should have a .profile file in your home directory. Alternatively, you could just create a symlink to the utility django-admin.py in /usr/bin, but I recommend the former approach.

Grab and install MySQL

I would normally recommend PostgreSQL, at least until we have DB2 on Mac, but I realize that many of you use and prefer MySQL, which also seems to be the only one that requires special instructions due to a few installation issues when trying to get MySQL and Python to work together. You can install MySQL by grabbing and running one of the packages that are available on the official site. Choose the one for x86 and Mac OS X 10.4.

Install the MySQLdb driver

Get MySQL-python-1.2.2.tar.gz from SourceForge. Please follow these exact instructions because the source code won’t compile out of the box and will give you the following error when trying to build it:

/usr/include/sys/types.h:92: error: duplicate 'unsigned'
/usr/include/sys/types.h:92: error: two or more data types
in declaration specifiers
error: Setup script exited with error: command 'gcc' failed

Run the following:

tar xvfz MySQL-python-1.2.2.tar.gz
cd MySQL-python-1.2.2

At this point, edit the _mysql.c file and comment out lines 37, 38 and 39 as follows:

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

Now, from the MySQL-python-1.2.2 folder run:

python setup.py build
sudo python setup.py install

If you still get an error (and only in that case) you’ll need to edit the site.cfg file within the same folder and set threadsafe = False, before running the two commands above once again.
If instead, you don’t receive an error but you see warnings about files not required on this architecture, don’t be concerned about them. The last step required is to create a symbolic link with the following command:

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

All these adjustments are required because we are building and installing the driver on Mac and not on Linux.

Verify the installation

You should be all set now, but let’s verify this right away. Open Terminal and run the following commands in the python shell (start this with the python command).

Verify that MySQLdb is correctly installed:

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

Now, verify that Django is working:

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

By running exit() get out of the python shell, and verify that django-admin.py is in your path:

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

If you obtain a similar output for all three of them, you are really set to write the next YouTube.

Where to go from here

Now that Django is installed, you can go read the Django Book 1.0 that’s available for free online. Something equally well done and useful is really missing from the Rails community. Above all, experiment, Django (and programming in general) is learnt by doing. The Definitive Guide to Django: Web Development Done Right is also available for purchase in its deadtree version, which just came out. It’s cheap and it’s already a best seller on Amazon. Despite the availably of a free version online, I like having paper versions of tech books so that I can read without staring at the monitor. Furthermore, I feel like rewarding the authors (who are also the framework creators), while encouraging publishing companies that are willing to allow authors to make their books available for free on the web. Well done guys!

Get more stuff like this

Subscribe to my mailing list to receive similar updates about programming.

Thank you for subscribing. Please check your email to confirm your subscription.

Something went wrong.

31 Comments

  1. C8E December 22, 2007
  2. Michael January 18, 2008
  3. Michael January 18, 2008
  4. Michael January 18, 2008
  5. k00k January 27, 2008
  6. Brandon Taylor February 2, 2008
  7. mikeal February 5, 2008
  8. Ryan Donohue March 1, 2008
  9. Erik March 6, 2008
  10. sofia March 7, 2008
  11. Brandon March 23, 2008
  12. Brandon March 23, 2008
    • Sushil August 30, 2011
      • Sushil August 30, 2011
  13. Jason May 2, 2008
  14. Sebastian May 10, 2008
  15. Joel May 10, 2008
  16. Joel May 10, 2008
  17. ace place May 20, 2008
  18. Lindsay June 2, 2008
  19. joel June 11, 2008
  20. Kevin M August 19, 2008
  21. Jason September 19, 2008
  22. Pingback: django on leopard | alkalias October 10, 2008
  23. Pingback: luminescente » Django November 10, 2008
  24. frink December 8, 2008
  25. Sami December 29, 2008
  26. Nikita January 3, 2009
  27. scrapmaster January 4, 2009
  28. Carl G March 9, 2009
  29. mika April 4, 2009
  30. Daniel S June 18, 2009
  31. Andy February 21, 2010

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.