How to debug Python scripts and API code in the console and in VS Code

Learn different ways to debug your Python code

Lynn G. Kwong
12 min readDec 26, 2021

Debugging is an important step in Python programming, whether you are a Python data scientist or software developer. It can be fairly straightforward to debug simple Python scripts. However, it is more advanced to debug Python API code, especially with VS Code. In this article, the most common ways to debug Python scripts and API code are introduced, both in the console and in VS Code. You shall then be fairly clear about which tools to use and how to use them in your practical work.

Image by OpenClipart-Vectors on Pixabay

How to debug Python scripts with pdb in the console?

In the repo cloned, there is a simple script called python_script.py, where the command line arguments are simply echoed:

To debug a script, the most “naive” way is to use print wherever necessary. This, however, is not convenient. A much better way is to use the pdb module. pdb is the built-in Python debugger that can be used to debug Python programs interactively.

--

--

Lynn G. Kwong
Lynn G. Kwong

Written by Lynn G. Kwong

I’m a Software Developer (https://youtube.com/@kwonglynn) keen on sharing thoughts, tutorials, and solutions for the best practice of software development.

Responses (1)