Notes and guides on programming topics

Virtual Environments with virtualenv

virtualenv

virtualenv is a third party Python package for handling isolated virtual environments. It is more feature-rich than the standard library package venv.

virtualenvwrapper-win

virtualenvwrapper-win, is an extension package to virtualenv, which provides easy-to-use wrappers around the commands.

Most of the commands shown below are for virtualenvwrapper-win

Note

This package works only in the regular Windows Command Prompt, not in PowerShell. The -win suffix, which is necessary for the Windows version of the package.

First time setup

Install the two packages if they aren’t already:

pip install virtualenv
pip install virtualenvwrapper-win

Creating a virtual environment

mkvirtualenv <new_env_name>

The new virtual environment is then created and automatically activated.

The active environment is shown in parenthesis in the left side of the prompt.

Pair environment with working directory

Pair an environment with the current working directory by

setprojectdir .

This pairs the active environment to the current working directory.

Deactivating an environment

To deactivate an environment that is currently active, simply close the prompt that has the environment open. The parenthesis on the left showing the active environment will have disappeared once a new prompt is opened.

Alternatively, if you want to continue working in the same prompt, deactivate directly by

deactivate

Obviously, virtualenvwrapper-win knows exactly what environment is active, and thus which one to deactivate.

Start working on an existing environment

To start working on a project that is tied to an existing environment

workon <env_name>

The current directory will be changed to the root directory of the project that is paired with the virtual environment.

Remove an environment

rmvirtualenv <env_name>

Useful commands

List all virtual environments

lsvirtualenv

See the packages installed in the active virtual environment

pip list

Note on using virtualenv in PowerShell

As mentioned above, PowerShell is not supported to virtualenvwrapper-win.

Although, virtualenv does support PowerShell. However, it has a more verbose syntax since it misses a wrapper package like virtualenvwrapper for Bash and virtualenvwrapper-win for Windows Command Prompt.

E.g. an environment can be activated by running the activate.ps1 PowerShell script tied to it.

Indices and tables