Create a default django development environment on python virtualenv

Recently, I decided to build my development environments on a python virtualenv, so project dependencies will not clobber global python path. Here is how I start a default django project from scratch:

First we should install virtualenv package if it is not installed

sudo apt-get install virtualenv

At the end of this process we should have folowing directory structure

my_project
----env
----project`
  • 'my_project' will be our root directory. Inside root directory there will be two other directories
  • 'env' will be the directory that holds our virtual environment
  • 'project' will be our actual project directory.

Ok. Lets start creating our environment. First, we should create our root directory

mkdir my_project
cd my_project

Now create our virtual environment

virtualenv env --no-site-packages

--no-site-packages parameter tells virtualenv command that we do not want this project to use global python path at all. In other words, if you install project dependencies on your global path but on your virtualenv, your project will not be able to find them. Therefore, you must install all dependencies on your virtual environment. Now we should activate our virtual environment to install django.

source env/bin/activate

To activate our environment, we called "activate" executable from bin directory of our environment. Now, your comment prompt should indicate that you activated your virtual env

(env)huseyin@home:/tmp/projects/my_project$

You can install your dependencies on your environment using easyinstall or pip. Since you activated your virtual environment, pip and easyinstall commands will run the ones from env/bin directory instead of global counterparts. After activating our environment,we can now install Django 1.3.

pip install Django==1.3

Since our virtual env is activated this command will install Django on virtual environment. Lastly we create our project

django-admin startproject project

We just created our project folder with the default project files inside it. At this point, you should have your default development environment. You just have to remember to activate your environment before starting development ( source env/bin/activate ). After this point I also add a git repository ,README file and .gitignore file. I also add django debug toolbar and django command line extansions to my environment. Lets start with the README file. Go to your project folder (cd project) and create your README file. I will use following content as a start point.

How to install virtual environment
* Create Virtual environment
    virtualenv env --no-site-packages
* Activate Virtual environment
     source env/bin/activate
* Install Dependencies
      pip install django==1.3
      pip install psycopg2
      pip install django-profiles

      pip install ipython (for development)
      pip install django-extensions
      pip install werkzeug
      pip install django-debug-toolbar-django13

Create a .gitignore file with following content

*~
*\.pyc

Initiliaze a git repository in current working directory

git init

We can also install extra packages for development. You don't have to install them on production

 pip install ipython
 pip install django-extensions
 pip install werkzeug
 pip install django-debug-toolbar-django13

(werkzeug is a debugger that is used by django-extansions)

You have to modify your settings.py file to use django-extensions and django-debug-toolbar. I will add how to enable them for your project when I have time.

blog comments powered by Disqus