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?


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

#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/ startproject $proj

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

#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
source bin/activate



And you're ready to go!


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.