Aveek Das
Debug python scripts in VS Code

How to debug Python scripts in Visual Studio Code

July 8, 2020 by

In this article, I am going to explain how we can easily debug Python scripts using the Visual Studio (VS) Code. In my previous article on this topic, I have explained how to set up a development environment to start coding in Python. I would definitely recommend reading that article if you have not setup your Visual Studio Code environment yet. This article can be considered as a continuation of the above-mentioned steps as I am assuming that you are already into programming in Python using the VS Code.

The need for debugging code

While we talk about how to debug Python scripts in VS code, it is essential for us to know why we should debug our code in the first place. Not only for Python, debugging is related to every other programming language in the world, starting from C, C++, JavaScript, and so on. In the software programming world, debugging is the process that allows the programmer to identify and locate bugs/errors in the existing code. Debugging is an integral part of any software development life cycle and sometimes it can take more time to debug code than to write the same.

Is debugging the same as testing?

Not really, debugging and testing are two different terms and should not be confused with one another. Let me clear this out. Suppose you are developing a new project and you have written all your code and as per the business requirements the code works well. Now, once you are done with the development, this code will be assigned to the QA engineer whose job is to test the application that you just developed. Let us assume that although the application works fine functionally, there is some issue due to which a calculation is being wrongly displayed. The QA engineer will then identify this as a defect/bug in your code and assign it back to you so that you can fix it.

Now, when you reproduce the code on your machine, your job is to identify the piece of code which is responsible for those calculations and identify the glitch in there. This process of identifying the correct code and trying to fix it with or finding another workaround is known as debugging. Although Visual Studio Code is capable of debugging most of the programming languages, we will use Python in this tutorial.

Debug Python scripts in VS Code

Assuming that you have already set up your Python development environment, let us now understand how to debug Python scripts from VS Code directly. I will begin by writing some sample code that can be used for demonstration purposes. Please note that we will only focus on debugging and writing the Python script is out of scope for this article.

Head over to the Visual Studio Code and create a new Python file. You can also use an existing file if you already have one. I am using the following code for demonstration purposes.

As you might be aware, you can just run this code by simply selecting Run Python File in Terminal from the context menu or by clicking on the Run icon on the top right corner. The following will be the result after the script is successfully executed.

Debug Python scripts in VS Code

Figure 1 – Executing Python Code in the Terminal

Now that we know our code is executed, we can go ahead and run the debugger to test the code and the execution flow as well. You can start the debugger in the following ways.

  • Select Start Debugging from the Run menu
  • Selecting the Run icon from the left pane and click on Run and Debug
  • Pressing F5 on the keyboard

I would like to go with the second option and hit Run and Debug.

Debug Python Scripts in VS Code

Figure 2 – Debug Python Scripts in VS Code

As soon as you hit the Run and Debug button, a popup will appear in VS Code which will prompt you to choose the Debug Configuration that you would like to use. Let us go ahead with the Python File option for the time being. You can select other debug configurations based on the application that you are working with.

Selecting Debug Configuration in VS Code

Figure 3 – Selecting Debug Configuration in the VS Code

As you select the debug configuration in Visual Studio Code, you can see that the Terminal window opens up from the bottom and the debugger starts executing your code.

Debug Python scripts

Figure 4 – Debugger started

Let me quickly explain the various areas that you can see on the screen.

Debug Toolbar – This is the main toolbar that allows you to navigate along with your code as you try to debug it. There are six actions on this toolbar which are as follows:

Debug Toolbar

Figure 5 – Debug Toolbar

  • Continue/Pause (F5) – Allows you to pause or continue the debugging process
  • Step Over (F10) – Allows you to move over to the next line of code
  • Step Into (F11) – Allows you to enter inside a different method during debugging
  • Step Out (Shift+F11) – Allows you to move to the parent stack
  • Restart (Ctrl+Shift+F5) – Restarts the debugging session from the beginning
  • Stop (Shift+F5) – Stops the debugging session

Variables Pane – Using the variables pane you can easily inspect the data elements within your program. When you start debugging a lot of system-defined variables are initiated along with the user-defined variables. During the debugging session, you can verify the values of each of those variables from this pane.

Debug Python Scripts

Figure 6 – Variables Pane in the VS Code

Watch Pane – Sometimes you may write a program with hundreds of variables within it. It is not possible to monitor the values of all those variables from the Variables pane as mentioned above. In such a case, you might want to monitor only one or two variables of your choice leaving the worry about the rest. You can add those variables to your watch list, and you can easily monitor the status and the values for those particular variables within this pane.

Watch Pane in VS Code

Figure 7 – Watch Pane in VS Code

Call Stack Pane – This is helpful when your code has a lot of inner methods and you navigate deep inside a stack and then you might lose track of your stack. When there is any error in your program you can easily know from which stack is the error has occurred and then debug it accordingly.

Breakpoints – This is an important concept while debugging code in any language. You may notice that while you initially started the debugger session, the terminal just executed the script and the debugging session stopped automatically. What if you want to stop the debugging session at some particular line and monitor the variables closely? In such a case, the breakpoints come to our rescue. You can attach a breakpoint to any line by just click on the left of the line number. A red dot will appear which means that the breakpoint has been defined for that line. The next time when you debug, you can see that the program stops the execution at the line and waits for your command. You can now use the Debug Toolbar to navigate along with your code.

Debug Python Scripts using breakpoint

Figure 8 – Breakpoints in VS Code

Conclusion

In this article, we have talked about the various topics on how to debug Python scripts in Visual Studio Code. As already discussed, Visual Studio Code is by far one of the simplest, yet powerful tools from Microsoft and used by many developers for the development of their applications. While writing complex applications, it often becomes a pain point to identify bugs in the existing system. In order to understand the complete flow and remove those bugs, debugging helps us to identify those pain points and understand them with more details about the execution flow of the application. Debugging should be considered as one of the core parts of any application development.

Table of contents

Setting up Visual Studio Code for Python Development
How to debug Python scripts in Visual Studio Code
Deploy Python apps to Azure Functions using Visual Studio Code
Getting started with Amazon S3 and Python
Getting started with Pandas in Python
Working with Pandas Dataframes in Python
Exploring databases in Python using Pandas
Best practices to follow while programming in Python
Exporting data with Pandas in Python
Create REST APIs in Python using Flask
Working with JSON data in Python
Understanding *args and *kwargs arguments in Python
Aveek Das
Python, Visual Studio

About Aveek Das

Aveek is an experienced Data and Analytics Engineer, currently working in Dublin, Ireland. His main areas of technical interest include SQL Server, SSIS/ETL, SSAS, Python, Big Data tools like Apache Spark, Kafka, and cloud technologies such as AWS/Amazon and Azure. He is a prolific author, with over 100 articles published on various technical blogs, including his own blog, and a frequent contributor to different technical forums. In his leisure time, he enjoys amateur photography mostly street imagery and still life. Some glimpses of his work can be found on Instagram. You can also find him on LinkedIn View all posts by Aveek Das

168 Views