DEV Community

John Nyingi
John Nyingi

Posted on

Debugging Python in VS Code

debug

I've covered getting started with installing Pep8 and Pylint in your virtual environment. So you've written some Flask/Django/Normal Python code and would like to debug your application. How do you go about it? Well this post will paint a vivid picture of how to do it.

First you need to open a terminal in VS Code you can do this by using ctrl + escape
Once the terminal is opened enable your virtualenv if you named your virtualenv env then the command will look like


$ source env/bin/activate

Enter fullscreen mode Exit fullscreen mode

We can now enable debugging in vscode you can do this by using f5 or
by clicking debug from menu tab the start with debugging
debug start
When activated you should see a debug panel. The python package for vscode comes with a number of pre-configured debug environments for different python frameworks. We need to set environment variables to debug our application, from the new debug panel select the settings icon, then add environment variables to run your application in. For my case I added env and defined some environment variables to run my Flask app.
add env

From the python drop down list select the debug environment of your choice. This will allow the application run in a debug environment bundling up the requirements set to run the application in that environment.
enable debug

Finally, you can now set breakpoints which will allow you to follow method calls and application running cycle. You can add a breakpoint by clicking on the left position of a line number of the line you want to break/pause during debugging. When the debug pauses it should look something like this.
breakpoint
You can follow method calls using the debug controls provided at the top.
debug controls
Starting from the left;

  • The first control continues running and ignores that breakpoint
  • The second control ignores method calls in that breakpoint and jumps to the next line of code
  • The third control follows the method calls in that line of code
  • The fourth control returns to the parent method call
  • The fifth control restarts the debugging session
  • The sixth control stops the debugging session.

Note:

Debugging in VS Code applies to all supported languages and the settings may vary.

Top comments (0)