Installing and Using Virtualenv Wrapper

Isolating Python environments is a very necessary aspect of developing applications with python. From having a specific version of Python being run in a container-like structure called virtual environments to contained installations of various python packages, virtual environments are very important.

There are numerous variations of virtual environments, even python has built in implementations of a virtual environment like; venv, but i do not personally like the idea of having your virtual environment folder and files in the same directory as your project/application. That's where virtualenvwrapper comes in, as the name suggests, it is just a wrapper around virtualenv and as such, it provides more commands and extended functionalities around virtualenv, which makes working with virtual environments more pleasant and organized.

Installing virtualenvwrapper

Make sure you have virtualenv installed.

$ pip install virtualenv

The installation is similar for both macOS and Ubuntu. Install virtualenvwrapper

pip install virtualenvwrapper

Most people like specifying the folder which holds the created virtualenvs, but i personally leave it to it's default ~/.virtualenvs which is created by default. If you want to change the home for your virtualenvs.

$ export WORKON_HOME=~/<folder_name>

Add it to your path or to the end of the .bashrc file so the virtualenvwrapper commands are loaded

$ source /usr/local/bin/virtualenvwrapper.sh

or

$ sudo nano ~/.bashrc

then add . /usr/local/bin/virtualenvwrapper.sh to the end of the .bashrc file.

Restart you terminal or reload the .bashrc by running . .bashrc in your home folder.

Virtualenvwrapper Commands

Virtualenvwrapper provides various commands for creating, stopping, switching, virtual environments.

Creating a virtualenv

To create a virtualenv with virtualenvwrapper

$ mkvirtualenv nosahama

New python executable in /Users/Nosa/.virtualenvs/nosahama/bin/python
Installing setuptools, pip, wheel...done.
virtualenvwrapper.user_scripts creating /Users/Nosa/.virtualenvs/nosahama/bin/predeactivate
virtualenvwrapper.user_scripts creating /Users/Nosa/.virtualenvs/nosahama/bin/postdeactivate
virtualenvwrapper.user_scripts creating /Users/Nosa/.virtualenvs/nosahama/bin/preactivate
virtualenvwrapper.user_scripts creating /Users/Nosa/.virtualenvs/nosahama/bin/postactivate
virtualenvwrapper.user_scripts creating /Users/Nosa/.virtualenvs/nosahama/bin/get_env_details

There is a little trick i use to create an environment and then set it's project folder to the current folder using -a .

$ mkvirtualenv nosahama -a .

New python executable in /Users/Nosa/.virtualenvs/nosahama/bin/python
Installing setuptools, pip, wheel...done.
virtualenvwrapper.user_scripts creating /Users/Nosa/.virtualenvs/nosahama/bin/predeactivate
virtualenvwrapper.user_scripts creating /Users/Nosa/.virtualenvs/nosahama/bin/postdeactivate
virtualenvwrapper.user_scripts creating /Users/Nosa/.virtualenvs/nosahama/bin/preactivate
virtualenvwrapper.user_scripts creating /Users/Nosa/.virtualenvs/nosahama/bin/postactivate
virtualenvwrapper.user_scripts creating /Users/Nosa/.virtualenvs/nosahama/bin/get_env_details
Setting project for nosahama to /Users/Nosa/Projects/nosahama-com

notice the last line Setting project for nosahama to /Users/Nosa/Projects/nosahama-com, this sets the current folder as the project folder.

Stopping a virtualenv

$ deactivate

Switching between virtualenvs

$ workon <virtualenv-name>

So if we have 3 different virtualenvs; venv1, venv2, venv3, we can switch between them with

$ workon venv1
(venv1) $
$ workon venv2
(venv2) $
$ workon venv3
(venv3) $

Using Projects

Virtualenvwrapper can also automate the process of creating a project folder and it's corresponding virtualenv. We just have to provide the path to our projects folder in our .bashrc file before the virtualenvwrapper command. So edit the .bashrc file and add

PROJECT_HOME='/path-to-project-folder'

Creating and Using a Project

$ mkproject <project-name>
$ workon <project-name>

For windows users, you can use virtualenvwrapper-win. You can read more on virtualenv and virtualenvwrapper.

Show Comments