Quick Ref: Python Virtualenv use

Supposing you have a clean install of a Debian-based distro such as Debian 7 proper or Xubuntu 14.04, this notes will walk you through setting up a Python development environment where you can leverage the power of Virtualenv through Virtualenvwrapper.

Why virtualenv?

When you code in python you will quickly find out that different projects have different requirements for the python version you need to use and also the versions of libraries you need to use. Virtualenv will allow you to compartmentalize the requirements on a per project basis without making you install libraries and python versions globally or system-wide.

Ok, ok... Here are the goods...

You have your new shinny install of Debian which comes with a fairly recent version of python pre-installed (somewhere above 2.7.3). Now you need to install a package manager for python and the one I use is pip.

sudo apt-get install python-pip

Once you have pip installed, you will need to also install virtualenv, like so:

sudo pip install virtualenvwrapper

Virtualenvwrapper is a very nice layer on top of virtualenv that makes working with it much more intuitive and convenient.

Next, you may want to automate a couple of things like I did. First, create a ~/.virtualenvs directory. This is where virtualenvwrapper will keep track of your environments. Then open you .bashrc file and add a few lines to it:

export WORKON_HOME=$HOME/.virtualenvs
if [ -f /usr/local/bin/virtualenvwrapper_lazy.sh ]; then
    . /usr/local/bin/virtualenvwrapper_lazy.sh

Now you are ready to start working, so open a new terminal, setup a virtual environment, and start cranking out some python code. Here's a quick reference for virtualenvwrapper:

Start new virtual environment?

mkvirtualenv [env-name]

Leave the current virtual environment?


List all available virtual environments?


lsvirtualenv -b

Enter an existing virtual environment?

workon [env-name]

List packages installed in current virtual environment?

pip freeze

Remove a virtual environment?

rmvirtualenv [env-name]

Start a virtual environment with specific python version?

mkvirtualenv [env-name] -p /usr/bin/python3.4

Replace [env-name] above with your actual environment name and enjoy!


Comments powered by Disqus