Setting up Python for Django webapps

This document describes how to initially set up your Python programming language environment, in order to develop Django-based webapps for CollectionSpace.

This is an initial, one-time-only setup process. You'll need to complete the steps in this document, just once, regardless of whether you plan to set up:

  • A personal development environment for writing and testing webapps; and/or
  • A production environment for running webapps.

Requirements

The following instructions assume:

  • Your host computer is running Debian, Ubuntu, or a similar APT-compatible Linux operating system distribution, one that includes a package manager like apt-get or aptitude. (You would need to translate these instructions to work with other Linux distributions, or with OSes like Mac OS X or Microsoft Windows.)
  • The system Python version is between 2.6.5 and 2.7.x. (The following instructions conservatively assume the use of Django 1.5 and the aforementioned Python versions. Newer version(s) of Django also work with Python 3.x.)

Procedure

Verify that Python is installed

Django 1.5 requires any Python version from 2.6.5 to 2.7.x.

  • To check whether Python is installed and is a required version, enter the following at your shell prompt:
    python -V
    
    You should see output similar to this:
    Python 2.7.1
    

Install Pip

Pip is a tool for installing and managing Python packages

  • To install Pip:
    sudo apt-get install python-pip python-dev build-essential
    

    It's recommended that you install the python-dev and build-essential packages along with python-pip. If they're not also installed, later on you won't be able to install any Python module that ships with a C extension.
  • Upgrade Pip:
    sudo pip install --upgrade pip
    

More documentation on Pip.

Install virtualenv

virtualenv is a tool for creating isolated Python environments for each of your projects' dependencies. It creates a private installation directory for each project, and, when a particular environment is activated, appends this directory to your $PATH.

virtualenv (as well as virtualenvwrapper, below) is not a strict requirement for developing Django-based webapps. However, it is commonly used by Python developers and is extremely helpful in keeping environments separate between two (or many) projects.

  • To install virtualenv:
    sudo pip install virtualenv
    

More documentation on virtualenv.

Install virtualenvwrapper

virtualenvwrapper provides a set of wrappers to Virtualenv, making it easier to create, delete, and switch between virtual environments.

  • To install virtualenvwrapper:
    sudo pip install virtualenvwrapper
    

To configure virtualenvwrapper for your Linux user account, you'll need to specify the path location where your virtual environments will be stored, via the WORKON_HOME environment variable. You'll also need to set up your shell to run ("source") the virtualenvwrapper.sh shell script each time you log in.

  • Set up virtualenvwrapper by adding these lines to your login shell's startup file (such as .bash_profile, if you're using the bash shell; e.g. via vi ~/.bash_profile):
    # The following line will store your virtual environments
    # in an ".envs" directory within your home directory
    export WORKON_HOME=$HOME/.envs
    source /usr/local/bin/virtualenvwrapper.sh
    
  • Then reload your startup file to make these settings active right away:
    source ~/.bash_profile
    

More documentation on virtualenvwrapper.