Virtual environments with Python, Pip, and VEnv

In Python development, a virtual environment is an isolated context for installing Python packages. As of Python 3.3, venv is included to help manage virtual environments.

Create and use a virtual environment

To create a virtual environment, it's usually best to specify the Python version explicitly to ensure the environment is setup with the expected version:

python3.8 -m venv path/to/environment

A best practice is to use a virtual environment per project. A common convention to do so is to create the environment in a folder called venv directly in the project folder:

cd my-project
python3.8 -m venv venv

To use the virtual environment, it needs to be activated:

source venv/bin/activate
which python3 # confirm expected python version

Once activated, it's usually necessary to install the project dependencies which are most often referenced by a requirements.txt file:

python3 -m pip install -r requirements.txt

Finally, the virtual environment can be deactivated when it's no longer needed:

deactivate

Why virtual environments are important: Dependency hell

Dependency hell is the confusion caused when dependency needs conflict between projects, users, and systems.

Unlike most other programming language package managers that install packages in the current working directory, pip by default installs packages as globally as possible. When installing a package, pip will first try to install that package system-wide. If system-wide installation is not permitted, pip will install the package for the current user.

These "as global as possible" installations can cause dependency hell when:

  • Multiple projects have conflicting dependencies
  • Project dependencies conflict with system dependencies
  • Multiple users of the same system need conflicting dependencies
  • Projects need to be tested against different versions of Python
  • Projects need to be tested against different library versions

For these reasons, a best practice for Python and pip is to always use a virtual environment.

Deeper Knowledge on Python Virtual Environments

Pipenv

Pipenv

An alternative to pip and venv

Broader Topics Related to Python Virtual Environments

Pip: Python's Package Manager

Pip: Python's Package Manager

Python's default package manager

Python (Programming Language)

Python (Programming Language)

An object-oriented and functional programing language where whitespace matters

Python Virtual Environments Knowledge Graph