This a template you can use for setting up a virtual environment for Python and Jupyter Notebook.
A virtual environment (venv) is an isolated Python installation for one project.
Think of it like this:
- Your system Python = the kitchen
- A virtual environment = a lunchbox for one project
Each lunchbox contains:
- Its own Python interpreter
- Its own installed packages (
pip install ...) - Its own package versions
Without a virtual environment:
- All projects share the same Python packages
- Installing or upgrading a package for one project can break another
- Version conflicts happen very easily
Example problem:
- Project A needs
Django==3.2 - Project B needs
Django==5.0
π Virtual environments solve this by isolating dependencies per project.
When you activate a virtual environment:
pythonandpippoint to the projectβs local versions- Packages install into
.venv/instead of system Python - Nothing outside the project is affected
When you deactivate it:
- Everything goes back to normal
This is the best-practice setup for macOS (M1/M2/M3).
Check:
python3 --versionIf itβs missing or very old, install via Homebrew:
brew install pythonOpen your project folder in VS Code.
In the VS Code terminal:
cd your-project
python3 -m venv .venvThis creates:
your-project/
βββ .venv/
βββ main.py
βββ requirements.txt
.venv is the standard name and is auto-detected by VS Code.
source .venv/bin/activateYouβll see:
(.venv) your-project %
That means itβs active.
-
Press Cmd + Shift + P
-
Search for Python: Select Interpreter
-
Choose:
.venv/bin/python
VS Code will now:
- Run code using the venv
- Install packages into the venv
- Use correct linting & autocomplete
π‘ VS Code usually remembers this automatically.
--_
pip freeze > requirements.txtThis allows others (or CI) to recreate the environment.
To reinstall later:
pip install -r requirements.txtdeactivate.venv/python app.py
pip install numpyThat installs packages globally and defeats the purpose.
.venv/β isolated Python + packagesactivateβ βuse this Python nowβ- VS Code interpreter β editor knows which Python to use
requirements.txtβ reproducible environment
You need three things, all installed inside your virtual environment:
- Jupyter (or JupyterLab)
- ipykernel (connects your venv to Jupyter)
- VS Code Python + Jupyter extensions (editor side)
In VS Code terminal:
source .venv/bin/activateConfirm:
which pythonIt should point to:
your-project/.venv/bin/python
Inside the venv:
pip install jupyter ipykernelThatβs it for Python-side installs.
What each does:
jupyterβ runs notebooksipykernelβ lets this venv appear as a selectable kernel
Install these once:
- Python (by Microsoft)
- Jupyter (by Microsoft)
VS Code will usually prompt you automatically.
-
Create a file:
notebook.ipynb -
Or:
Cmd + Shift + P β Jupyter: Create New Notebook
In the top-right of the notebook:
-
Click Select Kernel
-
Choose:
Python (.venv)
Now:
- All cells run using your virtual environment
pip installinstalls into.venv
Always do this with the kernel selected or in the terminal with the venv activated.
pip install numpy pandas matplotlib%pip install seaborn%pip, not !pip, in notebooks.
Run this cell:
import sys
print(sys.executable)Expected output:
.../your-project/.venv/bin/pythonβ That confirms Jupyter is using the correct environment.