Debugging Python code with vscode


Overview

Replace the traditional print("DEBUG: " + str(variable)) with vscode breakpoints and variable inspection.

Details

Basic Debugging

Start with a Python script by running it in the built in terminal (click the play button in the upper right corner):

To enable debugging, click the Run and Debug button in the left side bar:

Click Run and Debug:

Select Python file:

Set a break point by clicking the space directly to the left of the line number in the Python code. A small red dot is added at every break point:

Click the step button to start. Each press of the step button will execute the code until the breakpoint is reached:

Expand the Variables section in the left hand debugging menu to examine individual variables:

In larger code bases it may be easier to view variable values by hovering the mouse over a variable name:

Django Debugging

Enabling debug mode in a Django environment will automatically start the debug server.

Start by selecting the Run and Debug:

Select the location of the Django manage.py file:

The debug server is automatically started: