Image from Wikimedia Commons

Virtual Environments in Python with venv

Table of Contents

Python’s built-in venv module makes it easy to create virtual environments for your Python projects. Virtual environments are isolated spaces where your Python packages and their dependencies live. This means that each project can have its own dependencies, regardless of what other projects are doing.

Create a Virtual Environment

Create an environment with:

python -m venv ./venv
python -m venv /path/to/venv

Activate an environment with:

source venv/bin/activate
source /path/to/venv/bin/activate

Make sure to test if python and pip are indeed in the environment by typing:

which python
# /absolute/path/to/venv/bin/python
which pip
# /absolute/path/to/venv/bin/pip

Install packages from a requriments.txt with:

pip install -r requrirements.txt

Deaktivate an environment with:

deaktivate

Resources