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
Pythonextension (ms-python.python) first:- Without the Python extension installed in VS Code,
Python: Select Interpreterand automatic terminal activation are unavailable.
- Without the Python extension installed in VS Code,
- Why
.venv(with a leading dot) is the recommended folder name:- VS Code’s Python extension automatically discovers virtual environments named
.venvorvenvinside${workspaceFolder}without requiring manual path browsing, and.venvis ignored by default in standard Python.gitignoretemplates.
- VS Code’s Python extension automatically discovers virtual environments named
- 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
- When VS Code opens a PowerShell terminal and runs
- Why an already-open terminal doesn’t show
(.venv)immediately after switching interpreters:- Selecting an interpreter via
Python: Select Interpreteractivates 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).
- Selecting an interpreter via
- Use
python -m pip install ...inside the VS Code terminal:- Even when
(.venv)appears in your terminal prompt, runningpython -m pip install <package>guarantees packages install into the exact interpreter selected in VS Code’s bottom-right status bar.
- Even when
- Conda, Poetry, and
uvenvironments work the exact same way:- Once created (
uv venv,poetry install, orconda create -n myenv python=3.12), runPython: Select Interpreter—VS Code detectsuv.venv, Poetry cache paths, and Conda environments automatically.
- Once created (
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. |
You must log in or register to comment.
