Environment Structure for Django Apps

I’ve been writing applications off and on for nearly 4 years now, since before Django 1.0 was even released. I must say the framework could not be better described than by its own tagline “The Web framework for perfectionists with deadlines”. Among the things I love about it are:

  • Amazing documentation, there’s not 30 different blog posts with different ways to do things, either read the django project documentation or the app documentation
  • They don’t consistently break backwards compatibility. While at times they do this, it is not the norm or standard, which isn’t the case for some other unnamed frameworks
  • DRY, Don’t Repeat Yourself. If there’s an app that exists people don’t have a burning need to recreate the same functionality, resulting in lower number but higher quality pluggable apps.

However, after using the framework for nearly 4 years I’m just now discovering my preferred way of managing environments. I know there’s still a bit of back and forth on development environment/IDE, but as far as configuring actual project environment I’ve become very comfortable with what I’ve now been using for many months. It also allows for someone else bootstrapping their environment incredible quickly as well. Below is a quick cookbook of how to do this on OSX and Ubuntu.

OSX with Macports:

1
2
3
$ sudo port install python27
$ sudo port install py27-virtualenv
$ sudo port install postgresql90

Now for setting up your project:

1
2
3
4
$ mkdir example
$ cd example
$ virtualenv-2.7 --no-site-packages .
$ source bin/activate

OSX with Brew:

1
2
3
$ sudo brew install python
$ sudo brew install virtualenv
$ sudo brew install postgresql

Now for setting up your project:

1
2
3
4
$ mkdir example
$ cd example
$ virtualenv --no-site-packages .
$ source bin/activate

Ubuntu:

Now for setting up your project:

1
2
3
$ sudo aptitude install python
sudo aptitude install virtualenv
sudo aptitude install postgresql

Now for setting up your project:

1
2
3
4
$ mkdir example
$ cd example
$ virtualenv --no-site-packages .
$ source bin/activate

For those of you not familiar virtualenv is a self-contained python environment. It holds its own copy of python and any libraries you install. Now that you’ve setup your virtualenv we can go through the process of installing django and setting up your repository. This is the same across all of the above platforms:

Add to a .gitignore file:

.gitignore
1
2
3
4
5
6
bin
build
include
lib
.Python
*.pyc

Add to a requirements.txt file:

requirements.txt
1
2
Django==1.3.1
psycopg2==2.4.1

Then run:

1
$ bin/pip install -r requirements.txt

This should fully installed any of your required apps and makes it easy for others to do the same to begin contributing to a larger app. Finally if you like you can create your git repo from this an make your first commit:

$ git init
$ git add .
$ git commit -m 'my first django virtualenv'

Comments