Saturday, May 31, 2008

Setting Up Django in Eclipse with PyDev on OS X

This is the first in a series of posts on my experience getting Django up and running for development on OS X.

Platforms:
OS X Leopard 10.5.2
Eclipse 3.3.2
PyDev 1.3.17
Django 0.96.2
Via MacPorts:
PostgreSQL 8.3
py25-psycopg2 2.0.5.1

Getting Started:

1) Install Eclipse in a place where the path has no spaces.



Make sure to install Eclipse without a path that includes spaces. You'll get errors when you try to run the Python interpreter within Eclipse if you don't.

2) Install PyDev


Following the guide at PyDev did me wonders.

Once you've got a successful 'Hello World' you're ready to move on to Django!

3) Django Pre-Install



Since I want to change the world with database-driven applications, I'll assume that you do too. If, for some reason, you're not going to run Django off a database, you can skip this section.

3.1) Pick a database:


Django supports PostgreSQL, SQLite 3, and MySQL.

If you're just starting out and want to just get things running, you can take advantage of SQLite's integration in Python 2.5 and get on with things.

The Django team seems to know what they're doing, and they prefer PostgreSQL, so I'm going to take that route and see where I end up. It also helps that it's free.

3.2) Installing PostgreSQL via MacPorts



The simplest path I've been able to find for installing PostgreSQL is through the MacPorts package manager. I used version 1.6.0 for Leopard. MacPorts will calculate and install dependencies for you, so you'll be able to install everything needed to interface with Django in one fell swoop. Be aware of one potential gotcha... you might need to restart Terminal for the following commands to work:

sudo ports install py25-psycopg2 +postgresql83

Installs the PostgreSQL 8.3 database and the interface for Python 2.5. (At this point, psycopg2 install depends on the desired version of PostgreSQL, hence the '+postgresql83'.)

I ran into an apparently minor hiccup in my install, so to complete the install, I ended up running the above command twice. Saw I probably needed the PostgreSQL server configuration too so afterwards, I ran this command:

sudo port install postgresql83-server


To create a database instance, you'll need to set up a 'postgres' user on your machine. Since NetInfo has been depricated in Leopard in favor of the Accounts preference pane, open System Preferences and add a user with the name 'postgres'.

Note: the following instructions assume you want to install your database in:

/opt/local/var/db/postgresql83

If you want to put your database somewhere else, just substitute your path in the commands below.

Then you set up a database:

sudo mkdir -p /opt/local/var/db/postgresql83/defaultdb
sudo chown postgres:postgres /opt/local/var/db/postgresql83/defaultdb
sudo su postgres -c '/opt/local/lib/postgresql83/bin/initdb -D /opt/local/var/db/postgresql83/defaultdb'


This sets up a directory '/opt/local/var/db/postgresql83/defaultdb', gives the postgres user ownership permissions, and then initializes the database.

To start postgres, you'll need to start PostgreSQL as the postgres user. Instead of typing out long commands to start, stop and get status reports on PostgreSQL, add some entries to ~/.profile to make it a bit more straightforward:

export PATH=$PATH:/opt/local/lib/postgresql83/bin
export PATH=$PATH:/opt/local/bin/:/opt/local/sbin
alias pgstart="sudo su postgres -c 'pg_ctl -D /opt/local/var/db/postgresql83/defaultdb -l /opt/local/var/db/postgresql83/defaultdb/logfile start'"
alias pgstop="sudo su postgres -c 'pg_ctl -D /opt/local/var/db/postgresql83/defaultdb stop -m fast'"
alias pgstatus="sudo su postgres -c 'pg_ctl status -D /opt/local/var/db/postgresql83/defaultdb'"


Now you can start (pgstart), stop (pgstop), and get the status (pgstatus) of the PostgreSQL server from the command line with some short and sweet commands.

After you have made these additions to your .profile in ~/, you'll need to restart terminal to see the changes. As of this post, you should get this response when you type 'python' into the terminal prompt:

Python 2.5.2 (r252:60911, Jun 1 2008, 21:59:53)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>


On to Django!

4) Installing Django (For real this time!!)



The instructions provided on www.djangoproject.com are pretty sufficient, but since we've installed Python 2.5 with MacPorts, we need to provide Django with a symbolic link in python's site packages directory, a link in the site packages to the trunk, and a link to django-admin.py to the system environment to allow the creation of Django projects without having to type out the full path to the script.

Note: I put Django in ~/Development/django/src/, so replace that in the commands below with the location on your machine.

sudo ln -s ~/Development/django/src/django-trunk /opt/local/lib/python2.5/site-packages
sudo ln -s /opt/local/lib/python2.5/site-packages/django-trunk/django /opt/local/lib/python2.5/site-packages/django
sudo ln -s /opt/local/lib/python2.5/site-packages/django/bin/django-admin.py /opt/local/bin
sudo ln -s /opt/local/lib/python2.5/site-packages/django-trunk/django/bin/django-admin.py /usr/bin


Now you should be able to enter:
ls -l /usr/bin/django-admin.py
into the command line and have it return something like this:

lrwxr-xr-x 1 root wheel 78 Jun 2 03:56 /usr/bin/django-admin.py -> /opt/local/lib/python2.5/site-packages/django-trunk/django/bin/django-admin.py


Likewise, you should be able to check Django's version in the python interpreter like so:

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


5) Setting up a project in PyDev



First we need to set up a project.

Resources:
Getting Started with PyDev.
Django on OS X Tiger with PostgreSQL.
Paydirt!! => Django Dev Environment On MacOS Tiger.
Where to find psychopg2 Documentation.
Installing Django with Pydev installed.
Bits and Pieces of an Eclipse + Pydev setup on OS X.
How to locate a the Python interpreter on OS X 10.5.2.
Good info on Django on OS X with PostgreSQL.