06-Jun-2022| Debugging, Dev Tools, Tutorials |6 min read

Python has become one of the most popular programming languages globally and is particularly popular in data science and artificial intelligence. Python’s popularity can be attributed to its ease of use and readability and the large ecosystem of libraries and frameworks built around it. Python is also popular among developers working on cloud-based applications, as they can use it to orchestrate complex workflows.

Netflix, for example, is one of the largest online streaming services in the world and relies heavily on Python for its content management system and recommendation engine. Python is also used extensively throughout Netflix’s back-end infrastructure, from system administration tools to deployment scripts. Major companies and institutions such as IBM, Pixar, Facebook, Spotify, and NASA also use Python. 

What is python?

Python is a programming language permeating every industry due to its versatility and large developer community. It is a general-purpose programming language employed in software development, scripting, and automation. It is also popular for scientific computing and data analysis, as it has many libraries and tools for these tasks.

Python is easy to learn for beginners because of its high readability and simple syntax construction. According to Statista, Python claimed the top spot for the most popular programming language in early March 2020. The open-source language also has an estimated market value of over $10 billion. In addition, TechRepublic reports Python as one of the most in-demand and employable skill sets from 2021 onwards.

Python meme

While knowing how to code in Python is a great skill to have – the ability to figure out what’s wrong with your python code is equally important.

Here is a guide on using VSCode to help you debug your Python code.

Step-by-step guide on debugging python in VSCode

Debugging is a staple requirement of every programming job. To debug means to walk through the program step-by-step to figure out the origins of an error. When we need to debug python, it’s usually because something is not working as expected.

In this tutorial, we will go over how to use VSCode’s in-built debugging tool to help us figure out and pinpoint where our errors are originating.

Prerequisites

For this tutorial, here are the prerequisites and assumptions:

  • you have a working knowledge and understanding of Python
  • that you have a Python interpreter installed

Opening a python project in VSCode

If you don’t have a python project to debug, here is one we prepared for you. It is a simple piece of python with an error built into the do_something_cool method.

 class User:
    def __init__(self, name, email):
        self._name = name
        self._email = email
 
    def get_name(self):
        return self._name
 
    def get_email(self):
        return self._email
     
    def do_something_cool(self):
        print ("Sign up to Lightrun " + self._namee)
 
    def __str__(self):
        return self._name + " , " + self._email
 
    users = [ User("Lightrun Demo", "help@lightrun.com"), User("Debugger", "debugger@debugger.com")]
 
    for user in users:
        user.do_something_cool()

Let’s get into the process of debugging this code.

Initial configurations

Start by clicking on the debug icon on the left-hand panel of your VSCode. Click on create a launch.json file, which will give you a drop-down of all your currently installed debuggers.

Screenshot

 

By default, a Python debugger is not installed in VSCode. You can identify this in the missing Python option in the dropdown pictured above.

To download and install a Python debugger, you can do one of the following:

  • Go back to the left-hand menu and click on the extensions button, then type Python in the search box, and a list of results should show up. 
  • Click on Install an extension for Python.

Screenshot

Regardless of what you choose, it will bring up similar, if not the same results. Select the first one, Microsoft’s official Python extension for Visual Studio Code. Click on the install button to add it to your VSCode instance.

Screenshot

Under normal circumstances, you shouldn’t have to restart VSCode. Go back to the debug menu. When you click on create a launch json file again, a Python option should be available. Alternatively, VSCode may simply make the selection for you and automatically generate a json file.

Screenshot

Debugging current file

Open up the file you want to debug. In our case, it is the file with the code example above. A faded red dot will appear when you hover over on the left-hand side of the line number. If you click on this dot, it will set a breakpoint.

Screenshot

A breakpoint is a point in the code where the program’s execution will stop temporarily so that the program’s state can be examined. Breakpoints are helpful for debugging purposes because they allow the programmer to check the values of variables and see how the program is executing at that point.

When you click on Run and Debug for the first time, you will get the option to select a debug configuration. For this tutorial, select Python File.

Screenshot

When you click on the play button located on the top left above the VARIABLES panel, the debugger will run through the code and stop at the breakpoint.

Screenshot

A debugging toolbar will also become available at this point. The debugging toolbar contains buttons that let you go forward, backward, and jump into different parts of the code based on where you currently are in your breakpoint. Metadata about your code will also appear in the VARIABLES panel, where locals and globals can be watched and monitored for changes.

Here is a rundown on what each icon in the toolbar does:

  • Continue (F5) – this will run past the breakpoint and continue with the rest of the program until it hits the next breakpoint.
  • Step over (F10) – this will take the debugger down to the following line.
  • Step into (F11) – this will take the debugger into the following function.
  • Step out (F12) – this will take the debugger out of the function and into the next step.
  • Restart (Ctrl+shift+F5) – restart the entire debugger.
  • Stop (shift+F5) – stop the debugging process and exit it.

Let’s run our debugger through, and we see that the variable for name doesn’t exist.

Screenshot

Debugging code using the Lightrun tool

If you’re a developer, you know that debugging code can be a real pain. It can be time-consuming and frustrating, especially if you’re working on a large project with a lot of code. Fortunately, there are now tools available to make debugging much easier and faster. One of these tools is Lightrun.

Lightrun is a real-time code debugger that you can use with any language. It integrates with popular IDEs and text editors, so you can use it with the tools you’re already familiar with.

With Lightrun, developers can add logs, snapshots and metrics to their live apps on-demand, and continuously examine the state of their program. Its functionalities include examining variables, viewing the call stack, and more, all with minimal footprint. Lightrun also makes it easy to step through code, so developers can see exactly what is happening at each stage.

Eyal Katz
Eyal is a digital marketing consultant at Lightrun and avid tandom bike rider.

All the Lightrun News That’s Fit to Print(f)!

Get the latest posts, news,
feature releases, updates, and articles directly to your inbox