I swore I wouldn't

Icon

Oh no, I'm blogging now?

Installing django with MySQL on OS X

I am experimenting with the django web-framework for a few projects at work.  I do the majority of my work in PHP or Java, but occasionally I like to play with Ruby on Rails, and decided that django deserved to have a look as well.

The first step in this process is to get django and the various components installed.

  • Python
  • EasyTools
  • MySql
  • MySqlDB
  • Django

Python – I do my development from a relatively new Macbook Pro, and as such, Python 2.6.1 is already installed.  This is confirmed by typing ‘python’ from a terminal window.

> python

Python 2.6.1 (r261:67515, Jul  7 2009, 23:51:51)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type “help”, “copyright”, “credits” or “license” for more information.
>>> ^D

EasyTools – Download the OS X of setup tools from http://pypi.python.org/pypi/setuptools#files, then run the .egg file as if it were an executable:

sh setuptools-0.6c9-py2.4.egg

MySql - I use MAMP (http://www.mamp.info) for my local PHP development (not sure how anybody could live without MAMP running locally.) MAMP provides a drop-in MySQL installation that I use for all of my local development purposes.  The one caveat to this is that it is not uncommon to run into scenarios where something is looking for a mysql_* executable that isn’t being found in the default location.

I previously was using MAMP for these installations, but found that at times, header files were needed for other installations, and MAMP isn’t really intended for this type of usage.  I still think MAMP is a great tool, but in this case, I needed to Install Mysql from source.

Next, add the path to the mysql_* executables to your PATH.

> export PATH=/usr/local/mysql/bin:$PATH

MySqlDB – In order for python/django to be able to talk with MySQL, the MySQL DB driver needs to be installed.  This is an area that caused me a little bit of delay, as the installation of MySQL-python 1.2.3c1 was complaining about my lack of gcc.  I encountered a few posts that described how to get around this limitation with earlier versions of MySQL-python, but given that I do a lot of development, it felt like the right thing to do would be to install the Mac OSX Developer Tools.

> curl -O http://pypi.python.org/packages/2.6/M/MySQL-python/MySQL_python-1.2.3c1-py2.6-linux-i686.egg
> sudo easy_install MySQL_python-1.2.3c1-py2.6-linux-i686.egg

If you receive errors about mysql, or gcc, then you need to re-visit your environment setup.

Django – Once these pieces were in place, installing the actual django source code was trivial.

> svn co http://code.djangoproject.com/svn/django/trunk/ django-trunk

Next, make sure that the Python interpreter can load Django’s code. There are various ways of accomplishing this. One of the most convenient, on Linux, Mac OSX or other Unix-like systems, is to use a symbolic link:

> ln -s `pwd`/django-trunk/django SITE-PACKAGES-DIR/django

Finding SITE-PACKAGES-DIR -
To find the location of the SITE-PACKAGES-DIR on your system, execute the following command from the command line:

> python -c “from distutils.sysconfig import get_python_lib; print get_python_lib()”

Add Django commands to PATH – I find it helpful to add the django bin directory to my PATH environment variable so that I have convenient access to the framework’s various commands:

> export PATH=SITE-PACKAGES-DIR/django/bin:$PATH

Earn $$$ – Now that these pieces are in place, you can create your first django project by issuing the command:

> django-admin.py startproject mysite

Overall the installation was a snap, on par with a Ruby on Rails installation.  Over the next few weeks, I will start developing some simple django projects and write a post describing the experience.

Category: Host, Infrastructure

Tagged: , , ,

Leave a Reply