venv β quick introΒΆ
What is a venv?ΒΆ
A virtual environment is a self-contained folder with its own Python interpreter and libraries. It keeps each projectβs dependencies isolated β so updating one project wonβt break another.
π Official docs
When to use itΒΆ
- Always if a project needs extra Python packages.
- Collaboration: teammates install the same dependencies in their own venv.
- Deployment: reproducible installs.
Basic usageΒΆ
1. Create and activateΒΆ
Windows (PowerShell)
macOS / Linux (bash)
1.5 Create and Activaet (external)ΒΆ
macOS / Linux (bash)
Create
Activate2. Upgrade pipΒΆ
3. Install dependenciesΒΆ
Case A: pyproject.toml
[project]
dependencies = ["numpy", "requests"]
[project.optional-dependencies]
dev = ["pytest", "black"]
Install both runtime + dev tools:
π‘ ".[dev]" means: install this project (".") + optional dev dependencies ([dev]).
Case B: requirements.txt
4. DeactivateΒΆ
External venv in VS CodeΒΆ
- Create venv outside repo, e.g.
~/.venvs/example-app.
- Windows:
C:\Users\<you>\.venvs\example-app\Scripts\python.exe - macOS/Linux:
/home/<you>/.venvs/example-app/bin/python
- Point VS Code to it via
.vscode/settings.json:
(on Windows)
(on macOS/Linux)
- Result: VS Code uses that interpreter for terminals, debugging, and Jupyter β without hardcoding usernames.
Daily workflowΒΆ
WindowsΒΆ
Inside repo venv
Using external venv
macOS / LinuxΒΆ
Inside repo venv
Using external venv
Then run Python as usual (python, pytest, etc.).
Exit with deactivate.