Python on VS Code
Why Python, Jupyter, and VS Code?
Python is an interpreted, general-purpose, object-oriented, high-level programming language with dynamic semantics. It has a strong position in scientific computing with a large community of users and available documentation. With an extensive ecosystem of scientific libraries and environments such as Numpy, Scipy, and Matplotlib, it allows user to work on various scientific topics, especially computer science, mathematics, statistics, and optimisation.
Jupyter provides the web-based notebook application, which allows user to write, run, display, and document the output produced by the code. This means that we are able to capture the entire workflow in a single file, which can be saved, restored, and reused later on.
With Python and Jupyter extensions installed on VS Code, we can create Jupyter notebook, interactive programming and computing that supports IntelliSense, debugging and more. The Python extension generally supports code completion and IntelliSense using the currently selected interpreter. IntelliSense is a general term for features, including code completion, parameter info, quick info, and member lists, which are very useful for developing Python applications.
Why WSL2? (Windows Users Only)
Windows users will use WSL2 to run a Linux environment inside Windows.
This helps ensure that:
- Python packages install and behave consistently.
- Command-line tools behave as expected.
- Everyone works in a similar computational environment.
You will still use VS Code on Windows, but your code will run inside the Linux environment.
Mac users do not need WSL2.
System Architecture Diagram (with WSL)
When using Windows + WSL + VS Code + Jupyter Notebook (Python), you are working across two operating systems.
The diagram below explains how and where your code actually runs.
Windows (GUI layer)
└── VS Code
└── Remote - WSL Extension
└── Ubuntu (Linux inside WSL2)
└── Python
└── Jupyter Notebook (renders and executes code)
└── HTML OutputWhat is Actually Happening
- In this setup, Windows is not running yourP ython code. It is only hosting the editor (VS Code).
- WSL2 is a lightweight virtual machine running a Linux kernel.
- Ubuntu is your Linux environment. This is where:
apt install,git,python, andvenvare actually running. - If you use Python, you may create a virtual environment (
.venv), which is where your actual runtime lives.
What Happens When You Click “Render”?
When you render a .ipynb document:
Windows GUI
↓
VS Code (connected to WSL)
↓
Jupyter Notebook (running inside Ubuntu)
↓
Python executes your code
↓
HTML document is generatedSo execution happens inside Linux. The final result is an HTML file.
Takeaway:
- Always keep your project inside:
/home/<user>/.... NOT:C:\Users\...Because crossing the boundary slows things down and causes path confusion.
Setup Guide
This guide will help you set up the working environment for your lab exercises and project.
Step 1: (Windows Users Only) Enable WSL2 and Install Ubuntu
Mac users: Skip to Step 2.
Open PowerShell as Administrator, then run:
wsl --install -d Ubuntu
wsl --update- Reboot if prompted.
- Launch Ubuntu from the Start Menu and create your Linux user. Remember your password!
- Update Ubuntu and install other essentials:
sudo apt update && sudo apt upgrade -y
sudo apt install -y build-essential curl git zip unzip wget gdebi-core- To verify, run:
uname -a. You should see Linux information.
Step 2: Install and Setup VS Code
Download and install the latest version of VS Code at https://code.visualstudio.com/Download. If VS Code is previously installed, upgrade via CMD / PowerShell:
winget upgrade --id Microsoft.VisualStudioCodeIn VS Code:
- Install Remote - WSL extension from the Extensions tab (or press
Ctrl+Shift+X). - Connect to WSL by pressing F1 → WSL: Connect to WSL. Ensure the bottom-left corner shows “WSL: Ubuntu”.
Step 3: Install Python (inside WSL)
In VS Code:
- Check the bottom left corner that you are actually running in Ubuntu (WSL2).
- Open a New Terminal, check that your command line starts with
<username>@<desktop name>:. - If yes, you are currently working inside WSL. Run:
mkdir -p ~/code/stat2005 # creates directory code/stat2005
cd ~/code/stat2005 # changes directory to code/stat2005
sudo apt install -y python3 python3-pip python3-venv # installs python, pip, virtual environment
python3 -m venv .venv # initialises virtual environment .venv
source .venv/bin/activate # activates .venv
pip install -U pip # upgrades pip to the latest version
pip install ipykernel jupyter pandas numpy matplotlib # installs packages
python -m ipykernel install --user --name stat2005 # creates a new Jupyter kernel called stat2005Then, install Python and Jupyter extension from the Extensions tab.
Note:
source .venv/bin/activateactivates the Python virtual environment. Once activated, your terminal prompt changes to show the environment name (e.g., (.venv)), indicating that all Python commands now use the interpreter and packages inside this environment. This keeps your project’s dependencies isolated from the system Python.- Using a virtual environment supports reproducibility: someone else (or future you) can recreate the exact same environment and avoid the classic “it works on my machine” problem. The standard practice is to freeze your dependencies so others can install the precise versions you used:
cd ~/code/stat2005
source .venv/bin/activate
pip freeze > requirements.txt # save exact packge versionsThis creates a file listing pinned versions, e.g. pandas==2.2.0
- Install dependencies from
requirements.txt
On a fresh machine (or after creating a new .venv), run:
python -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
pip install -r requirements.txtThis recreates the same environment with the same package versions, ensuring your project behaves consistently across machines.
Step 4: Render
- Download the lab sheet
ws01-python.ipynbfrom BB and move it into your local lab folder. - Open the notebook in VS Code.
- Select interpreter and kernel:
- Press F1 → “Python: Select Interpreter” and choose
~/code/stat2005/.venv/bin/python. - In the top-right of the notebook interface, set
Kernel = stat2005(or whichever kernel is linked to your.venvinterpreter).
- Press F1 → “Python: Select Interpreter” and choose
- To convert
.ipynbto.html, run:
jupyter nbconvert ws01-python.ipynb --to htmlReferences
Python documentation Python Software Foundation. (2024). Python documentation. https://docs.python.org/
Jupyter documentation Project Jupyter. (2024). Jupyter documentation. https://jupyter.org/documentation
Visual Studio Code documentation Microsoft. (2024). Visual Studio Code documentation. https://code.visualstudio.com/docs
Windows Subsystem for Linux (WSL 2) documentation Microsoft. (2024). Windows Subsystem for Linux documentation. https://learn.microsoft.com/windows/wsl/