LinuxCommandLibrary

virtualenv

Create isolated Python environments

TLDR

Create a new environment

$ virtualenv [path/to/venv]
copy

Customize the prompt prefix
$ virtualenv --prompt [prompt_prefix] [path/to/venv]
copy

Use a different version of Python with virtualenv
$ virtualenv [[-p|--python]] [path/to/pythonbin] [path/to/venv]
copy

Start (select) the environment
$ source [path/to/venv]/bin/activate
copy

Stop the environment
$ deactivate
copy

SYNOPSIS

virtualenv [OPTIONS] ENV_DIR

PARAMETERS

ENV_DIR
    The directory where the new virtual environment will be created.

-h, --help
    Show help message and exit.

-v, --verbose
    Increase verbosity.

-q, --quiet
    Decrease verbosity.

-p PYTHON_EXE, --python=PYTHON_EXE
    The Python interpreter to use for the new environment (e.g., python3.9).

--system-site-packages
    Give the virtual environment access to the system site-packages dir.

--no-setuptools
    Don't install setuptools in the virtual environment.

--no-pip
    Don't install pip in the virtual environment.

--no-wheel
    Don't install wheel in the virtual environment.

--clear
    Delete the contents of the virtual environment directory if it exists.

--prompt PROMPT
    Provides an alternative prompt prefix for this environment.

DESCRIPTION

The virtualenv command is a tool to create isolated Python environments. These environments allow you to manage dependencies for specific projects without interfering with the global Python installation or other projects. This is crucial for ensuring project reproducibility and avoiding dependency conflicts, especially when different projects require different versions of the same package.

It works by creating a directory that contains all the necessary executables to use Python, as well as copies or symbolic links to the system's Python installation. When you activate a virtual environment, your shell's PATH is modified to prioritize the environment's Python interpreter and associated packages. This means that when you run python or pip, you are using the versions within the virtual environment.

Virtual environments are highly recommended for any Python project development as they streamline dependency management and prevent conflicts between libraries of various projects. Virtualenv ensures that each project has its own self-contained and isolated Python dependencies

<B>ACTIVATION</B>

To use a virtual environment, you need to activate it. This is typically done by sourcing the activate script in the environment's bin (or Scripts on Windows) directory. For example: source myenv/bin/activate. This modifies your shell's PATH. To deactivate the environment, run the deactivate command.

<B>WORKFLOW</B>

The typical workflow involves creating a virtual environment, activating it, installing dependencies using pip, and then working on your project. When you're done, you can deactivate the environment.

<B>VENV MODULE</B>

From python 3.3 and up the same functionality are included in venv module. You can create virtual environments without installing extra packages.
Example: python3 -m venv myenv

HISTORY

virtualenv was created by Ian Bicking and has been a popular tool for managing Python dependencies for a long time. Initially distributed independently, its functionality has been integrated into the venv module in Python 3.3 and later. However, virtualenv remains a widely used tool offering additional features and flexibility. The rise of virtualenv significantly improved the Python development workflow by making it easier to manage dependencies and ensure project isolation, preventing conflicts between different projects on the same system.

With time, virtualenv was integrated into pip which provides similar functionality

SEE ALSO

python(1), pip(1)

Copied to clipboard