Projects Blog About Contact

Install Django and create project with virtualenv with a simple shell script

Oct. 27, 2012

I always find myself creating quick and dirty Django projects to learn more about django and also to try out useful third party apps.

So I created a simple shell script to create a Django project on a development machine to prevent the same old process. After all, being a Django dev means one should practice the DRY principle too, right?

#!/bin/bash

#Take project name as input
if [ -z "$1" ] 
then
	echo "Enter project name"
	read proj
else
	proj="$1"
fi

#Create a virtualenv
virtualenv $proj
cd $proj
source bin/activate



#Install django within the virtualenv
echo "PIP_RESPECT_VIRTUALENV=true" >> bin/activate
touch requirements.txt
echo "django" >> requirements.txt
pip install -r requirements.txt

#Create project
python lib/python2.7/site-packages/django/bin/django-admin.py startproject $proj

#Set default environment variables
export DJANGO_SETTINGS_MODULE="$proj.settings"
export PYTHONPATH="$VIRTUAL_ENV/$proj"

#Write the variables into activate for future use.
echo "export DJANGO_SETTINGS_MODULE=$proj.settings" >> bin/activate
echo "export PYTHONPATH=$VIRTUAL_ENV/$proj" >> bin/activate

#Reset virtualenv and we are ready
deactivate
source bin/activate

Usage

source django_one_step.sh

And you're ready to go!

PS:

The procedure is inspired by this blog post which shows how to deploy django projects on Heroku (Btw you should read it even if you aren't planning on deploying on Heroku to learn some good practices if you're new to django like me) and this question on Stack Overflow which finally helped me get off my reverie on a sleepy Sunday.