04 — Visual Studio Code

Setup
VS Code
Python
Configure VS Code to use the course cv Conda environment, WSL, notebooks, and the Python debugger.

VS Code for this course

Visual Studio Code (VS Code) is the officially supported IDE for this course.

Download it from:

https://code.visualstudio.com/

This guide assumes that you have already created the course Conda environment named cv.

1. Install VS Code

Install VS Code on your main operating system.

Windows + WSL

Install VS Code on Windows. Do not install a separate Linux copy of the VS Code desktop application inside WSL.

The VS Code WSL extension will allow the Windows application to work directly with files and tools inside Ubuntu.

Official WSL documentation:

https://code.visualstudio.com/docs/remote/wsl-tutorial

3. Create a course projects folder

Keeping course projects together makes environments and file paths easier to manage.

Windows + WSL

Inside Ubuntu:

mkdir -p ~/cv_projects
cd ~/cv_projects

Then open the folder in VS Code:

code .

VS Code should open a new window connected to WSL.

Look at the bottom-left corner of VS Code. It should indicate that the window is connected to WSL, for example:

WSL: Ubuntu

macOS/Linux

Create a projects folder:

mkdir -p ~/cv_projects
cd ~/cv_projects
code .

You can also use File → Open Folder from VS Code.

4. Confirm the integrated terminal

Open a VS Code terminal:

Terminal → New Terminal

or:

Ctrl+`

On Windows with WSL, this terminal should be running inside Ubuntu.

Check:

pwd

Then activate the course environment:

conda activate cv

5. Select the cv Python interpreter

Open a Python file, then open the Command Palette:

Ctrl+Shift+P

Search for:

Python: Select Interpreter

Choose the interpreter associated with the Conda environment:

cv

The exact Python version shown next to the environment may change as the course environment is updated. The important part is that the selected environment is cv.

Important

Do not select an interpreter simply because its Python version looks correct. Make sure it belongs to the cv Conda environment.

6. If VS Code cannot find the environment

Activate the environment in a terminal:

conda activate cv

On macOS/Linux/WSL, run:

which python

Copy the returned path.

Then in VS Code:

  1. Open Python: Select Interpreter.
  2. Choose Enter interpreter path if necessary.
  3. Select or paste the Python path from the cv environment.

You can verify the selected interpreter in the integrated terminal:

python --version

and:

python -c "import sys; print(sys.executable)"

7. Test the environment

Create a file named:

test_environment.py

Add:

import numpy as np
import cv2

print("NumPy:", np.__version__)
print("OpenCV:", cv2.__version__)

Run the file using the Run Python File button or from the terminal:

python test_environment.py

If the imports work, VS Code is using an environment with the required packages.

8. Working with notebooks

Open a .ipynb file in VS Code.

At the top-right of the notebook, choose Select Kernel and select the Python environment associated with:

cv

Run a cell such as:

import sys
print(sys.executable)

The printed path should point into the cv environment.

9. Debugging Python

The VS Code debugger lets you pause code, inspect variables, and step through execution. This is often much more useful than adding many print() statements.

Set a breakpoint

Click in the margin to the left of a line number. A red dot indicates a breakpoint.

Start debugging

Press:

F5

If VS Code asks for a debug configuration, choose:

Python File

Debugger controls

The debugger lets you:

  • Continue — run until the next breakpoint;
  • Step Over — execute the current line without entering called functions;
  • Step Into — enter a called function;
  • Step Out — finish the current function and return to its caller;
  • Restart — restart the debugging session;
  • Stop — end the session.

The Variables panel shows local and global variables while execution is paused.

The Debug Console can evaluate Python expressions using the current program state.

10. A useful debugging example

Create:

import numpy as np

image = np.zeros((100, 200, 3), dtype=np.uint8)
image[:, :100, 0] = 255

mean_value = image.mean()
print(mean_value)

Set a breakpoint on:

mean_value = image.mean()

Start debugging with F5.

While paused, inspect:

image.shape

and:

image.dtype

in the Debug Console.

This is especially useful when working with images because shape and data type errors are common.

12. Common problems

Python works in the terminal but not when using the Run button

VS Code is probably using a different interpreter.

Use:

Python: Select Interpreter

and choose cv.

Notebook imports fail but Python scripts work

The notebook kernel may be different from the selected Python interpreter.

Use Select Kernel in the notebook and choose cv.

Windows user sees Windows paths instead of Linux paths

Make sure the VS Code window is connected to WSL. The bottom-left status area should indicate a WSL connection.

A reliable way to open the correct environment is:

cd ~/cv_projects
code .

from an Ubuntu/WSL terminal.

The debugger runs the wrong Python version

Check:

import sys
print(sys.executable)

Then reselect the cv interpreter.