Via Marconi 20, Bussolengo (VR)

Come creare multipli ambienti Django con virtualenv

Often we must work on various "Django": applications, each one dependent on different set of libraries. For examples, we are working on a Django 1.1 project and and we need to start a new one with the latest version of the framework. The best solution is to keep the various environments separate, in order to ensure that each project accesses only the libraries which it depends. Python offers us "virtualenv":, a tool to create multiple isolated environments. Each environment is completely independent from the others and from the installed packages in system's site-packages folder. Let's then see how to create a virtualenv and install the latest version of Django (today 1.2.1). h1. 1- Install python-setuptools The first thing to do is to install python-setuptools in order to have access to the command "easy_install": and to easly download packages. Alternatively, you can get the same results using the package "pip": Let us open a console and type the following commands: stefano@stefano-laptop:~$ sudo apt-get install python-setuptools Reading package lists... Done Building dependency tree Reading state information... Done python-setuptools is already the newest version. 0 upgraded, 0 newly installed, 0 to remove and 132 not upgraded. stefano@stefano-laptop:~$

h1. 2- Install virtualenv

The second step is to install the package virtualenv using easy_install:

stefano@stefano-laptop:~$ sudo easy_install virtualenv
[sudo] password for stefano: 
Searching for virtualenv
Best match: virtualenv 1.4.9
Processing virtualenv-1.4.9.tar.gz
Running virtualenv-1.4.9/ -q bdist_egg --dist-dir /tmp/easy_install-3D0IWT/virtualenv-1.4.9/egg-dist-tmp-xe4LRm
warning: no previously-included files matching '*.*' found under directory 'docs/_templates'
Adding virtualenv 1.4.9 to easy-install.pth file
Installing virtualenv script to /usr/local/bin

Installed /usr/local/lib/python2.6/dist-packages/virtualenv-1.4.9-py2.6.egg
Processing dependencies for virtualenv
Finished processing dependencies for virtualenv

h1. 3- Create the virtual environment

Once installed the package virtualenv we will need to create a space to host multiple environments. In my case, I've chose to create a directory in my home rather than in another writable folder in the filesystem. 

stefano@stefano-laptop:~$ cd Progetti/Python/
stefano@stefano-laptop:~/Progetti/Python$ mkdir virtualenvs
stefano@stefano-laptop:~/Progetti/Python$ cd virtualenvs/

Now we create the real virtualenv, called "Django-1.2-env. Note the argument added to the command virtualenv, --no-site-packages: so our virtual environment will have only a minimal set of libraries in his site-packages folder. We also note that all next commands will not need access as super user via sudo.

stefano@stefano-laptop:~/Progetti/Python/virtualenvs$ virtualenv --no-site-packages django-1.2-env
New python executable in django-1.2-env/bin/python
Installing setuptools............done.

h1. 4- Download Django in /tmp

Open a new console and download the tarball with the latest version of the framework Django. For simplicity, I've downloaded the package to /tmp.

stefano@stefano-laptop:~$ cd /tmp/
stefano@stefano-laptop:/tmp$ wget
--2010-08-28 11:20:58--
Connecting to||:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: [following]
--2010-08-28 11:20:59--
Connecting to||:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 6248006 (6.0M) [application/octet-stream]
Saving to: `Django-1.2.1.tar.gz'

100%[======================================>] 6,248,006    590K/s   in 12s     

2010-08-28 11:21:11 (523 KB/s) - `Django-1.2.1.tar.gz' saved [6248006/6248006]

h1. 5- Installing Django in virtualenv

At this point, unpack Django and activate the virtualenv (via the command "source  virtualenvdir/bin/activate"). Once activated virtualenv, we'll find his name in the shell: (django-1.2-env)stefano@stefano-laptop

stefano@stefano-laptop:/tmp$ tar xzfv Django-1.2.1.tar.gz
stefano@stefano-laptop:/tmp/Django-1.2.1$ source /home/stefano/Progetti/Python/virtualenvs/django-1.2-env/bin/activate
(django-1.2-env)stefano@stefano-laptop:/tmp/Django-1.2.1$ python install

We verify here that Django is installed properly in the virtual site-packages:

stefano@stefano-laptop:~/Progetti/Python/virtualenvs/django-1.2-env$ ls lib/python2.6/site-packages/
django           easy-install.pth   setuptools-0.6c11-py2.6.egg
Django-1.2.1-py2.6.egg-info  pip-0.7.2-py2.6.egg  setuptools.pth

h1. 6- Create a new project

At this point, we can create a new project using the command as required by the framework.

(django-1.2-env)stefano@stefano-laptop:/tmp/Django-1.2.1$ cd /home/stefano/Progetti/Python/
(django-1.2-env)stefano@stefano-laptop:~/Progetti/Python$ startproject newdjangoapp
(django-1.2-env)stefano@stefano-laptop:~/Progetti/Python$ cd newdjangoapp/
(django-1.2-env)stefano@stefano-laptop:~/Progetti/Python/newdjangoapp$ ls

In conclusion, we can say that virtualenv is an excellent solution to create environments fully independent and designed to accommodate each individual project. In this way, we avoid conflicts of libraries and have the opportunity to try new configurations without creating problems for other applications.