How to isolate python installations with virtualenv
1. How to isolate
python installations
with virtualenv
Divick Kishore,
iWish Inc, www.iwishapp.com,
divick@iwishapp.com
2. When to use virtualenv?
● If you don't have sudo or root access on a
development machine
● You want to try out a new package without
polluting the system directories / global
python installation
● You want to test your code with a new
version of a library without uninstalling the
previous version
● You want to test your code in an isolated
environment before you deploy it to a
production server
3. What is Virtualenv
● Tool to create isolated python environment
quite like chroot jail on Unix systems
● Difference being that program running in
chroot jail cannot access anything outside of
chrooted folder while virtualenv allows
installation of multiple versions of a single
library
4. Virtualenv setup and usage
● Install Virtualenv
$ pip install virtualenv
● Create a Virtualenv
$ virtualenv --no-site-packages ve
○ The name of virtualenv can be anything you like
○ --no-site-packages is optional as it is the default
○ The way it works is by creating symlinks to your
global python installation
● Use the isolated Python environment
○ To enter the environment run:
$ source ./ve/bin/activate
(ve) $
5. Enter the Virtualenv
● On entering the virtualenv, notice the
command prompt change. It has a (ve) prefix
now
$ source ./ve/bin/activate
(ve) $
● Before entering the virtualenv, see the list of
packages installed
$ pip freeze
Brlapi==0.5.5
CouchDB==0.8
Django==1.3
Fabric==1.3.4
...
6. Install packages in newly created
Virtualenv
● On entering the virtualenv, see the list of
packages installed
(ve) $ pip freeze
(ve) $
○ As you can see above, there are no packages
installed and you have a clean environment to begin
testing
● Now you can install any package that you
like which will get installed in the virtualenv
've' that you just created
(ve) $ pip install <package-name>
(ve) $ pip install -r requirements.txt
7. Exit VirtualEnv
● Simply run
(ve) $ deactivate
$
● See the command prompt change back to $
without 've' prefix
8. Summary
● Virtualenv and pip are must use tools for any
python developer
● It seem daunting at first, but both Virtualenv
and Pip are quite easy to setup and use
● Use Pip and Virtualenv in combination
earlyon to avoid headaches in long run