Configuring Python Environment With Virtualenvwrapper

A comfortable wrapper around python virtualenv.

Working on a python project in an isolated python environment is recommended so that python modules and packages don’t meddle with that of other projects or even that of the operating system. Thus, a virtual environment is needed eminently.

A good choice of virtual environment is the python VIRTUALENV which is a great tool for creating isolated python environments. An even better choice is the VIRTUALENVWRAPPER which is a set of extensions to the VIRTUALENV

Among the set of extensions are wrappers for creating and deleting virtual environments and otherwise managing your development workflow, making it easier to work on more than one project at a time without introducing conflicts in their dependencies. In this article, I would walk you through the following:

  1. Installing and configuring virtualenvwrapper to manage virtual environments in one place.
  2. Wrappers for creating, copying and deleting your virtual environments.
  3. Wrappers for navigating to and configuring projects for virtual environments.

It is assumed that python is installed on your computer. To install and configure virtualenvwrapper, do the following:

On your command terminal run the commands below:

cd ~
mkdir .virtualenvs
pip install virtualenv
which virtualenv
pip install virtualenvwrapper

The first line changes your current directory on the terminal to the home directory. The second line creates a hidden directory named virtualenvs where all created virtual environments would be stored. The third line installs virtualenv via pip. The fourth line tells you the directory where virtualenv was installed. The fifth line installs the virtualenvwrapper.
After running the commands above, open the .bash_profile file via vim or any other tool you prefer.

vim .bash_profile
# Press shift+I to add the next two lines to your .bash_profile
export WORKON_HOME=~/.virtualenvs
source /usr/local/bin/

The commands about would export your your WORKON_HOME variable, thus, point workon command to the virtualenvs folder. The source command would fire-up the virtualenvwrapper shell script for you. It’s good practice to put these commands in the .bash_profile since it runs all its commands at the start of the terminal.
After adding both lines, press the esc key then :wq character keys to save and exit the .bash_profile. Close and reopen your terminal when you’re done with all the steps above.

Note that all your virtual environments would now live in the ~/.virtualenvs folder. Now you have virtualenv installed and a great wrapper around it. Let’s explore it’s usage now. To create a virtual environment and activate it immediately, use the command below on your terminal:

mkvirtualenv createdenv

If you created your project folder already, you could cd into the folder before running the mkvirtualenv command with some arguments to automatically navigate to the project folder whenever you activate the virtual environment. Here is the format: mkvirtualenv [-a project_path] [-i package] [-r requirements_file] [virtualenv options] [ENVNAME]

mkvirtualenv -a $(pwd) createdenv


mkvirtualenv -a $(pwd) python=python3 createdenv

If you created your python environment first and made some module installations before creating your project folder and files e.g Django projects, no worries, you are not excluded in the goodies that come with automatically navigating to your project folder when you activate your virtualenv; Just run this command in the format- setvirtualenvproject [~/.virtualenvs/your-virtual-env/] [~/path/to/your/project]

setvirtualenvproject ~/.virtualenvs/your-venv/ ~/path/to/project/rootdirectory

Use the deactivate command to deactivate a virtual environment. Use the workon createdenv command to subsequently activate a virtual environment or just run workon to list all available virtual environments created. You can navigate to other created virtual environments while on a given environment without necessarily deactivating it first.

workon createdenv

You could use the rmvirtualenv ENVNAME to remove an installed virtual environment. Note that you must deactivate the virtual environment to be able to delete or remove it.

rmvirtualenv createdenv

There is this sweet command to copy an entire virtual environment to a newly created virtual environment. If you haven’t felt the need to do this, you certainly would someday. A typical scenario is when you need to observe the effect of meddling with specific modules in your current environment while preserving its original form. Here is the command to copy your virtualenv: cpvirtualenv [ENVNAME] [TARGETENVNAME]

cpvirtualenv createdenv copiedenv

There are several other interesting functionalities this wrapper(virtualenvwrapper) brings to the table of a world-class python developer; enjoy what you see as you explore.