Virtual Environments
Create and use venv with specific Python versions (Windows)
Why use a virtual environment?
- Isolate dependencies per project.
- Pin a Python version per project.
- Avoid version conflicts across tools.
Check installed Python versions
Use the Python Launcher on Windows. Lists discovered versions and install paths.
py -0p
Create a venv with a specific version
From your project folder. This creates a .venv folder in the project.
# Python 3.11 example
py -3.11 -m venv .venv
# Or Python 3.12
py -3.12 -m venv .venv
Activate / Deactivate (PowerShell)
If activation is blocked, allow local scripts:
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned
.\.venv\Scripts\Activate.ps1
# ...work inside the venv...
deactivate
Upgrade pip and install dependencies
python -m pip install --upgrade pip
pip install -r requirements.txt # if present
Freeze and restore requirements
# Save current environment
pip freeze > requirements.txt
# Recreate later
pip install -r requirements.txt
VS Code: select interpreter
- Open Command Palette: Ctrl+Shift+P
- Run “Python: Select Interpreter”
- Pick .venv\Scripts\python.exe