LinuxCommandLibrary

virtualenvwrapper

Manage isolated Python virtual environments

TLDR

Create a new Python virtualenv in $WORKON_HOME

$ mkvirtualenv [virtualenv_name]
copy

Create a virtualenv for a specific Python version
$ mkvirtualenv --python [/usr/local/bin/python3.8] [virtualenv_name]
copy

Activate or use a different virtualenv
$ workon [virtualenv_name]
copy

Stop the virtualenv
$ deactivate
copy

List all virtual environments
$ lsvirtualenv
copy

Remove a virtualenv
$ rmvirtualenv [virtualenv_name]
copy

Get summary of all virtualenvwrapper commands
$ virtualenvwrapper
copy

SYNOPSIS

virtualenvwrapper is not a single executable command but a collection of shell functions. Once sourced in your shell profile (e.g., ~/.bashrc), it provides the following common commands:

mkvirtualenv [options] <env_name>
workon [<env_name>]
rmvirtualenv <env_name>
deactivate
lsvirtualenv
cdvirtualenv
cpvirtualenv [options] <source_env> <new_env>

PARAMETERS

<env_name>
    The desired name for the virtual environment. Used by commands like mkvirtualenv, workon, and rmvirtualenv.

--python=<path>
    Specifies the Python executable to use for the new environment (e.g., --python=/usr/bin/python3.9). Primarily used with mkvirtualenv.

--system-site-packages
    Grants the virtual environment access to the global site-packages directory. Used with mkvirtualenv and cpvirtualenv.

<source_env>
    The name of an existing virtual environment to copy from. Used with cpvirtualenv.

<new_env>
    The desired name for the new virtual environment being created as a copy. Used with cpvirtualenv.

DESCRIPTION

virtualenvwrapper is a set of shell functions that extend Doug Hellmann's virtualenv (or Python 3's built-in venv module) to simplify the process of creating, deleting, and switching between Python virtual environments. It provides a convenient wrapper around the often verbose virtualenv commands, offering a streamlined workflow for developers managing multiple projects with distinct dependencies.

Key features include standardizing the location of all virtual environments (defaulting to ~/.virtualenvs), providing quick commands for common tasks like mkvirtualenv (create), workon (activate/switch), rmvirtualenv (delete), and deactivate (deactivate current environment). It also offers utilities such as lsvirtualenv (list environments), cdvirtualenv (change to active environment's directory), and cpvirtualenv (copy an environment). By integrating seamlessly with your shell (Bash, Zsh, Ksh), virtualenvwrapper ensures a consistent and efficient way to isolate project dependencies, preventing conflicts and maintaining a clean development setup.

CAVEATS

virtualenvwrapper relies on either the standalone virtualenv package or Python 3's built-in venv module being installed. It must be sourced in your shell's startup file (e.g., .bashrc, .zshrc) to make its commands available permanently. If not sourced, the commands will only be available in the current terminal session. Users should be aware that it modifies the shell environment, which can sometimes interact with other shell configurations if not set up carefully.

INSTALLATION AND CONFIGURATION

To install, use pip: pip install virtualenvwrapper. After installation, add the following lines to your shell's startup file (e.g., ~/.bashrc, ~/.zshrc), ensuring WORKON_HOME is set to your preferred directory for virtual environments:

export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 (optional)
source /usr/local/bin/virtualenvwrapper.sh

Reload your shell (e.g., source ~/.bashrc) for the changes to take effect.

IMPORTANT ENVIRONMENT VARIABLES

WORKON_HOME: Specifies the directory where all virtual environments will be stored (defaults to ~/.virtualenvs).
VIRTUALENVWRAPPER_PYTHON: Sets the default Python interpreter to use when creating new environments with mkvirtualenv.
VIRTUALENVWRAPPER_VIRTUALENV: Specifies the path to the virtualenv executable, if it's not in your PATH or you want to use a specific one.
VIRTUALENVWRAPPER_HOOK_DIR: Defines a directory for custom shell scripts (hooks) that run before or after virtualenvwrapper commands.

HISTORY

Developed by Doug Hellmann, virtualenvwrapper emerged as a solution to streamline the management of Python virtual environments, which could be cumbersome using virtualenv directly. It was first released in 2009 and quickly gained popularity within the Python community due to its intuitive interface and powerful features. Its development closely paralleled the increasing adoption of virtualenv and pip for dependency management, becoming an almost indispensable tool for Python developers seeking clean, isolated development environments. While initially a separate project, its functionality is now often considered a standard component of a well-configured Python development setup, continuously adapting to changes in Python versions and packaging tools.

SEE ALSO

virtualenv(1), python(1), pip(1), bash(1), zsh(1)

Copied to clipboard