Python Virtual Environment

It is generally not a good idea to install Python packages globally at the system level using pip (some distributions are starting to disallow this).

Instead, the system package manager [1] should be used. Another alternative is to use pip with the --user flag.

However, the recommended way is to use Python Virtual Environments. The venv package is now part of the Python Standard Library. To create a virtual environment run:

python3 -m venv .venv

This creates a virtual environment in the directory .venv.

To activate it, run the command for your shell:

Platform

Platform

Command

POSIX

Bash/Zsh

source .venv/bin/activate

Fish

source .venv/bin/activate.fish

Csh/Tcsh

source .venv/bin/activate.csh

PowerShell

.venv/bin/Activate.ps1

Windows

CMD.exe

.venvScriptsactivate.bat

PowerShell

.venvScriptsActivate.ps1

After activating the environment you can install packages inside it using pip:

python3 -m pip install <package>

Attention

For some reason venv is not included with Python on Debian/Ubuntu based distributions and has to be installed separately:

apt-get -y install python3-venv