Skip to main content

Using venv for Virtual Environments

venv creates a lightweight environment from an existing Python installation. Environments are disposable: record dependencies and recreate them rather than moving or committing the environment directory.

Create an environment​

From the project directory:

# macOS or Linux
python3 -m venv .venv
# Windows
py -m venv .venv

The .venv name is a convention, not a requirement. Add it to version control ignore rules.

Activate and verify​

# macOS or Linux
source .venv/bin/activate
python -c "import sys; print(sys.executable)"
# Windows PowerShell
.venv\Scripts\Activate.ps1
python -c "import sys; print(sys.executable)"

The printed path should point inside .venv. Activation prepends the environment's command directory to PATH; it is a shell convenience, not a security boundary.

You can skip activation and call the interpreter explicitly:

.venv/bin/python -m pip --version

On Windows, use .venv\Scripts\python -m pip --version.

Install and leave​

With the environment active:

python -m pip install "requests>=2,<3"
python -m pip check
deactivate

Deleting .venv removes the environment. Recreate it from the project's dependency declaration. Because scripts inside an environment contain absolute paths, environments are generally not portable to another location or computer.