Notes: installing Django / Python / Mysql on OS X

Trying to branch out in my scope of web development, being mostly a PHP and .NET developer, I decided to try Python. Now I still do not know much about Python development, packages, or even the syntax. Really I am just getting started with it. But I thought I would link to a couple of articles that helped me get setup to develop under OS X for anyone else starting out.

Why try Python?

I started watching some interviews on the yahoo developer network. One in that caught my attention was with Matt Mullenweg, founder of Automattic and developer of wordpress. When asked the question, if you were to start out on a project the size of wordpress today, is there any other language that you would use/consider? And sure enough he answered Python. For the pass couple of years I have thought about using python, and I even once installed it. But that has been as far as I have gotten.

I also started using the website Pownce.com recently. Pownce is a social network allowing you to send messages, files, links, images and video to your friends easily. Well, Pownce who just opened up their site to the public (they where in a private beta for about 6 months) uses the django framework for python for the whole site.

Last Friday I watched a video by Jacob Kaplan-Moss one the the lead developers of django, and found some the of the concepts really interesting. Django was and is developed out of Lawerance Kansas, you know the center of web development. It was built by the web team for the local newspaper in order to better mange the many sites they host and their constantly changing needs. Jacob explains that better than me, if you watch the video. I downloaded the Framework on Friday night (as I was driving back from Kansas City Saturday and Sunday) as something to look at on the way.

Getting Started

Well fortunately Python is included in Leopard, so I didn’t have to download that over my gprs connection in Kansas. Django is very easy to setup, basically run a script from inside the extracted django directory.

sudo python setup.py installOk with that installed where do you start? Django provides a nice walkthrough of setting up a project, what files are included and a step through of the built in features. After following about a page of the walkthrough I got to the point of being ready to sync my DB code with the real database, MySQL.

python manage.py syncdb

You then get this:

python manage.py syncdb
Traceback (most recent call last):
File "manage.py", line 11, in <module>
execute_manager(settings)
File "/Library/Python/2.5/site-packages/django/core/management.py", line 1672, in execute_manager
execute_from_command_line(action_mapping, argv)
File "/Library/Python/2.5/site-packages/django/core/management.py", line 1571, in execute_from_command_line
action_mapping[action](int(options.verbosity), options.interactive)
File "/Library/Python/2.5/site-packages/django/core/management.py", line 486, in syncdb
from django.db import connection, transaction, models, get_creation_module
File "/Library/Python/2.5/site-packages/django/db/__init__.py", line 11, in <module>
backend = __import__('django.db.backends.%s.base' % settings.DATABASE_ENGINE, {}, {}, [''])
File "/Library/Python/2.5/site-packages/django/db/backends/mysql/base.py", line 12, in <module>
raise ImproperlyConfigured, "Error loading MySQLdb module: %s" % e
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

So where as Python is installed by default under OS X, the mysql package is not.

How to install MySQLdb Package for Python.

First I went to the Mysql Downloads page and got a copy of the MySQLdb Package

Download the package, extract it, and open Terminal.app and change directories to the newly extracted package.

Here is where I ran into some fun, how do you install this? Well I found a website that suggested the Easy Installer…and that sounded nice, so I downloaded it first.

Download ez_setup.py, and run it; this will download and install the appropriate setuptools egg for your Python version.

There are many ways on install a package with the Easy Installer, I choose the directly from an unzip source folder, since that is what we have. And you can find more directions about the Easy Installer here

Now that you have Easy Installer, well installed, from terminal.app in the extracted MySQLdb Package MySQLdb folder run:
easy_install .

Got some errors, yeah we all did.

First, python will not be able to find your mysql installation (you did install mysql right?) If you didn’t get the .dmg file from the Mysql website, and install it through their very easy and very friendly installer, including a preference pane. The only problem with this it it puts itself in a non-standard folder, and that will cause a couple of issues.

Python will not be able to find mysql_config, or mysql for that matter because it is not in your path. To add it to your path add this line to the .profile file in your home directory.
PATH="${PATH}:/usr/local/mysql/bin"
Save that, and close terminal.app, and reopen it, This will cause the .profile to be re-read and your new addition to the path to be evaluated. Go ahead a try, now from the command line you should be able to call mysql_config, and any other mysql executable for that matter.

Try and run that easy_install command again, and yes you will still get errors.

Now you should get something like:
Running MySQL-python-1.2.2/setup.py -q bdist_egg --dist-dir /var/folders/At/At0AMaeKEAaYVW-LYfH6e++++TI/-Tmp-/easy_install-BUSsM_/MySQL-python-1.2.2/egg-dist-tmp-qFBtdU
In file included from /usr/local/mysql/include/mysql.h:43,
from _mysql.c:40:
/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 with exit status 1

Now to edit a couple of the files that came with the MySQLdb package. First _mysql.c
comment out the following lines:

#ifndef uint
#define uint unsigned int
#endif

Also edit the site.cfg file and change

threadsafe = True
to
threadsafe = False

Next, just one more Mysql Path fix run the following command:

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

Python will now be able to find the lib files where they should be. Ok that is it, run the easy_install . command and everything should go smoothly. As far a django is concerned, just continue to follow the walkthrough and you should do fine. I hope this helped.

Here are the websites that helped me, So if you run into any other issues these would be a good place to start: