Python Virtual Environments Made Easy (vscode python venv)
Tue Nov 11th, 2025 — 7 days ago

How To Use A Python Virtual Environment (vscode python venv)

See the companion Python venv tutorial deep-dive: Ultimate Python Venv Tutorial and Guide.

Problem

  • You need to isolate project-specific Python packages without touching the pip3 system-wide packages of Python.
  • You want consistent installs across machines and easy activation in terminals and VS Code.

Solutions

  • Create and activate a new environment.
# POSIX/macOS/Linux
python3 -m venv .venv
source .venv/bin/activate
python -V
python -m pip install --upgrade pip \
  setuptools wheel
  • Install dependencies from a file.
python -m pip install -r requirements.txt
  • Run a script with unbuffered output and UTF-8.
PYTHONUNBUFFERED=1 PYTHONUTF8=1 python -u your_script.py
  • Deactivate and remove the environment.
deactivate
rm -rf .venv/
  • Select the interpreter in VS Code.

    • Install the “Python” extension.
    • Press Command Palette → “Python: Select Interpreter” → choose ./.venv.
    • Enable auto-activation for new terminals:
{
  "python.defaultInterpreterPath": "${workspaceFolder}/.venv/bin/python",
  "python.terminal.activateEnvironment": true
}

What is a Python Venv?

A Python virtual environment is a lightweight, built-in tool that creates an isolated interpreter and site-packages directory per project.

How To Activate Python Venv

Let’s go over how to activate a Python venv using shell scripts that prepend .venv/bin to PATH so python and pip resolve inside the environment.

Python Venv Tutorial

Here are the steps from creation to activation, installing packages, running scripts, and deactivation.


Things To Consider

  • Use python3 -m venv .venv to create; use python -m pip ... after activation.
  • A venv affects only Python tooling on PATH. You can still use Go, Docker, Node, and Git.
  • Recreate the venv after upgrading the Python minor version.
  • In VS Code, creating via “Python: Create Environment” also works and pins the workspace.
  • Prefer .venv/ at the repo root and add it to .gitignore.

Gotchas

  • Do not execute ./.venv/bin/activate; always source .venv/bin/activate.
  • If prints are delayed, use python -u or set PYTHONUNBUFFERED=1.
  • Old pins may not support newer Python (e.g., 3.14). Bump versions or set bounds.
  • Do not put pip inside requirements.txt; use --upgrade-deps at venv creation or a constraints file.
  • In shells with custom prompts, disable Python’s default prompt injection if you add your own.

Sources


Further Investigation

  • pyenv or uv for managing multiple Python versions.
  • pipx for global-style CLI tools without touching project envs.
  • VS Code testing and debugger configs for module vs script entrypoints.

TL;DR

  • Create, activate, install, run, and clean up quickly.
python3 -m venv .venv \
  && source .venv/bin/activate \
  && python -m pip install -r requirements.txt \
  && PYTHONUNBUFFERED=1 python -u your_script.py
  • If installs fail, recreate with modern tooling.
rm -rf .venv \
  && python3 -m venv --upgrade-deps .venv \
  && source .venv/bin/activate \
  && python -m pip install -r requirements.txt