How to create multiple Django environments using 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).
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:
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:~$
2- Install virtualenv
The second step is to install the package virtualenv using easy_install:
[sudo] password for stefano:
Searching for virtualenv
Reading http://pypi.python.org/simple/virtualenv/
Reading http://virtualenv.openplans.org
Best match: virtualenv 1.4.9
Downloading http://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.4.9.tar.gz#md5=c49067cab242b5ff8c7b681a5a99533a
Processing virtualenv-1.4.9.tar.gz
Running virtualenv-1.4.9/setup.py -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
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:~/Progetti/Python$
stefano@stefano-laptop:~/Progetti/Python$ mkdir virtualenvs
stefano@stefano-laptop:~/Progetti/Python$ cd virtualenvs/
stefano@stefano-laptop:~/Progetti/Python/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.
New python executable in django-1.2-env/bin/python
Installing setuptools............done.
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:/tmp$ wget http://www.djangoproject.com/download/1.2.1/tarball/
--2010-08-28 11:20:58-- http://www.djangoproject.com/download/1.2.1/tarball/
Resolving www.djangoproject.com... 64.207.133.18
Connecting to www.djangoproject.com|64.207.133.18|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: http://media.djangoproject.com/releases/1.2/Django-1.2.1.tar.gz [following]
--2010-08-28 11:20:59-- http://media.djangoproject.com/releases/1.2/Django-1.2.1.tar.gz
Resolving media.djangoproject.com... 64.207.133.30
Connecting to media.djangoproject.com|64.207.133.30|: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]
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/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$
(django-1.2-env)stefano@stefano-laptop:/tmp/Django-1.2.1$ python setup.py install
We verify here that Django is installed properly in the virtual 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
6- Create a new project
At this point, we can create a new project using the command django-admin.py as required by the framework.
(django-1.2-env)stefano@stefano-laptop:~/Progetti/Python$
(django-1.2-env)stefano@stefano-laptop:~/Progetti/Python$ django-admin.py startproject newdjangoapp
(django-1.2-env)stefano@stefano-laptop:~/Progetti/Python$ cd newdjangoapp/
(django-1.2-env)stefano@stefano-laptop:~/Progetti/Python/newdjangoapp$ ls
__init__.py manage.py settings.py urls.py
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.