Saturday 7 September 2013

Using virtualenv to run multple python environments on the same machine

Virtualenv allows you to have multiple Django environments on one PC, each with it's own Django and Python settings and packages.

First install virtualenv:

  • pip install virtualenv

Create a virtual environment from the folder where you want the virtual env files:

  • virtualenv name

You run "scripts\activate.bat" script to activate the virtual environment:

  • cd name
  • scripts\activate.bat

Add the following to the scripts\activate.bat file to set the location of your apps settings.py file:
  • set DJANGO_SETTINGS_MODULE=<app>.settings

where <app> is the name of your django app.


Install the tools you need:
  • pip install django
...and any other Django/Python packages.  These should be documented in requirements.txt in the root of your project folder if you already have a project you want to install on your machine.


If using Pydev from Eclipse, set up the path to the virtual env's python.exe in the project preferences.  
Be sure to select the virtual env python from the Interpreter drop down list in the PyDev - Interpreter/Grammer window: