Better management of Python virtual environments with virtualenvwrapper

After writing about ‘what is virtualenv‘  and pyenv  I have been feeling a compulsion to write about the one remaining tool in the family i.e. virtualenvwrapper.
As it is in the name, virtualenvwrapper is just a wrapper (an extension) around the virtualenv. The sole motive behind writing it, is covering the discrepancies or features virtualenv lacks, and biggest issue is managing these virtualenvs. As stated in the official docs, the main value-adding features of virtualenvwrapper are:
  • Organizes all of the virtual environments at a single location.
  • Provides better management of virtualenvs – intuitive commands for creating, deleting, copying virtualenvs.
  • A single command to switch between environments i.e. workon (demonstrated later in the post)
  • User-configurable hooks for all operations (see Per-User Customization).
  • Plugin system for more creating shareable extensions (see Extending Virtualenvwrapper).


Installation and Setup: same as virtualenv, virtualenvwrapper is a python package, and can be installed via pip :

$ sudo pip install virtualenvwrapper
It’ll also install the dependencies:
 virtualenvwrapper install
For Windows, use virtualenvwrapper-win instead:
pip install virtualenvwrapper-win
Some one-time only initialization: the main file (wrapper script) is virtualenvwrapper.sh, you need to add the path to it in your shell startup file. You can get the path to it:
 

$ which virtualenvwrapper.sh
/usr/bin/virtualenvwrapper.sh
Next is, add the source path in your shell startup file i.e. ~/.bashrc – can use any file editor or simply execute the following in your shell:
$ echo 'export WORKON_HOME=$HOME/.virtualenvs' >> ~/.bashrc
$ echo 'export PROJECT_HOME=$HOME/projects' >> ~/.bashrc
$ echo 'source /usr/bin/virtualenvwrapper.sh' >> ~/.bashrc
Restart your shell or simple reload the .bashrc file:
$ source ~/.bashrc
 virtualenvwrapper install
WORK_HOME is the directory where virtualenvwrapper will be keeping all the venvs.
The above initialization has activated the script and made available the following commands:

Using virtualenvwrapper: let’s verify the setup and get our hands-on the new venv tool. Start with creating our first venv:

$ mkvirtualenv test_venv

(test_venv) $
mkvirtualenv command will create and activate new venv i.e. test_venv – For exiting the venv, use deactivate:
(test_venv) $ deactivate
$
 make virtualenvwrapper
To choose among the venvs we have workon <venv_name> command – if we execute it without specifying any venv, it’ll list all the available venvs:
$ workon
test_venv
To start using/activate a venv, simply:
$ workon test_venv
(test_venv) $
For deleting a venv:
$ rmvirtualenv test_venv
Removing test_venv...
 workon virtualenvwrapper

If I have missed something, here is the official documentation for the setup and usage – documentation.

For a full fledged sand-boxed development environment, check out Vagrant.

Leave a Reply

Your email address will not be published. Required fields are marked *