venv

Table of contents
  1. What is venv
  2. Basic usage
    1. Create environment
    2. Activate environment
    3. Deactivate environment

What is venv

venv is Python’s default virtual environment tool.


Basic usage

Create environment

venv creates a virtual environment for the Python version used to invoke it.

# If you only need one environment for the project
python -m venv venv
# If you are going to need multiple environments
python -m venv venv/myenv  # Creates a nested dir

python -m venv <name-of-env> creates a directory in cwd.

The <name-of-env> can be anything you like, but venv is used the most by convention.

Just like node_modules, because venv exists within the project root, it is almost always added to .gitignore.

Activate environment

Assuming cwd is project root where venv directory exists,

source venv/bin/activate

Once activated, which python will point to

/Path/To/Project/venv/bin/python

Deactivate environment

deactivate