How do you configure and activate a Python virtual environment in VS Code? | A1Lab@MZeeshanM to PythonEnglish • 2 days ago

How do you configure and activate a Python virtual environment in VS Code?

message-square
0

Create a project-local virtual environment (such as python -m venv .venv) in your workspace root, open the VS Code Command Palette (Ctrl+Shift+P on Windows/Linux or Cmd+Shift+P on macOS), and run Python: Select Interpreter to pick ./.venv/bin/python (or .\.venv\Scripts\python.exe on Windows)—which automatically activates .venv in every new integrated terminal (python.terminal.activateEnvironment is true by default) and persists the path in .vscode/settings.json.

Basic Example

# 1. Create a virtual environment in your project root folder (run in VS Code Terminal):
python -m venv .venv          # Windows (or `py -3 -m venv .venv`)
python3 -m venv .venv         # macOS / Linux

# 2. Recommended: Let VS Code create AND select the environment automatically:
# Press Ctrl+Shift+P (Win/Linux) or Cmd+Shift+P (macOS) -> run:
#   "Python: Create Environment" -> choose "Venv" -> select Python interpreter -> optionally check requirements.txt
// Optional `.vscode/settings.json` entry if you want to pin the interpreter explicitly for the workspace:
{
  "python.defaultInterpreterPath": "${workspaceFolder}/.venv/bin/python",
  "python.terminal.activateEnvironment": true
}

Key Details & Edge Cases

  • Install the official Microsoft Python extension (ms-python.python) first:
    • Without the Python extension installed in VS Code, Python: Select Interpreter and automatic terminal activation are unavailable.
  • Why .venv (with a leading dot) is the recommended folder name:
    • VS Code’s Python extension automatically discovers virtual environments named .venv or venv inside ${workspaceFolder} without requiring manual path browsing, and .venv is ignored by default in standard Python .gitignore templates.
  • Fixing Windows PowerShell PSSecurityException (Activate.ps1 cannot be loaded because running scripts is disabled):
    • When VS Code opens a PowerShell terminal and runs .venv\Scripts\Activate.ps1, Windows blocks unsigned scripts by default unless you run this once in PowerShell:
      Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
      
  • Why an already-open terminal doesn’t show (.venv) immediately after switching interpreters:
    • Selecting an interpreter via Python: Select Interpreter activates it for newly spawned terminals (and debug runs). Click the Trash icon on existing terminal tabs and open a fresh terminal (Ctrl+Shift+\``), or activate manually (source .venv/bin/activateon macOS/Linux,.venv\Scripts\activate` on Windows).
  • Use python -m pip install ... inside the VS Code terminal:
    • Even when (.venv) appears in your terminal prompt, running python -m pip install <package> guarantees packages install into the exact interpreter selected in VS Code’s bottom-right status bar.
  • Conda, Poetry, and uv environments work the exact same way:
    • Once created (uv venv, poetry install, or conda create -n myenv python=3.12), run Python: Select Interpreter—VS Code detects uv .venv, Poetry cache paths, and Conda environments automatically.

Comparison of VS Code Virtual Environment Workflows

Method Creates .venv Automatically? Installs requirements.txt During Setup? Auto-Activates New VS Code Terminals? Best Used When
Python: Create Environment (Command Palette) (Recommended) Yes (creates .venv + .gitignore) Yes (prompts to check requirements.txt / pyproject.toml) Yes Starting a fresh Python project in VS Code with zero manual terminal commands.
python -m venv .venv + Python: Select Interpreter Yes (via terminal command) No (run python -m pip install -r requirements.txt) Yes Standard CLI workflow or custom Python flags (--system-site-packages, --prompt).
Workspace .vscode/settings.json (python.defaultInterpreterPath) No (points to existing environment) No Yes Multi-root workspaces or teams standardizing on a shared relative .venv path across machines.
Manual source .venv/bin/activate in Terminal Only No No Current tab only (IntelliSense/debugger still use old interpreter) Temporary one-off terminal commands; avoid for regular editing because Pylance/debugger won’t see packages.