Sitemap
Karan Churi

Karan Churi

Dev @VMware

Debugging python shell

4 min readSep 26, 2022
Press enter or click to view image in full size
Photo by Alex Chumak on Unsplash

Debugging is a major skill that all software developers must cultivate. Having right tools or techniques can help us optimise the way we do it. Almost all popular IDE’s have GUI based debugging tool which not only make debugging more faster, but we get to know about the complete flow of the code.

This article focuses on debugging in python shell.

We can categories debugging into 2 sections.

  1. Involving code changes.
  2. Using debugger

Making changes in code to check for values/behaviour can be one approach to debug. This may involve following approaches

  1. Print statement
    This approach is super easy and gets it job done when the issue is not big or do not have multiple dependencies. Just add print statement wherever you need to peek 🫣 into variable values or want to know whether your code block is actually executed or not. Using logging library can be useful in such scenarios, but then too much of logging may also add up noise while actually checking logs for failure.
  2. Assert
    This is typically used for testing but can also be used for debugging certain scenarios where you'll know where the code is possibly breaking

Above approaches may be fast and sometimes it may get your job done, but in scenarios where the bug is caused due to many dependencies which even you're not aware of, debugging becomes difficult. Also sometimes we end-up writing many debug statements, where cleanup becomes problematic.

Using debugging tools can save your day in such situations. These tools allow us to pause the execution of code at certain points (breakpoint), getting to know the current value of variable in current block, executing your own statements to check and many more. Let's have a look on couple of these tools which can be beneficial for debugging in python shell.

  1. pdb/ipdb:
    Depending on python or ipython shell you're using you can this library to add breakpoints at certain location where you feel it’s possibly throwing error. We can execute the code one line at a time, to understand the flow and variables in the code. There's a huge list of other features this tool provides which can help us to debug our code without even touching the code base. The only drawback about pdb/ipdb is that it’s CLI based. So you better know all the commands to make the best out of it. While pdb come built-in with Python, to install ipdb we can use the following command.
pip install ipdb

2. Debugpy:
This tool is developed by Microsoft, which I personally find it very useful to debug in shell. The interesting part about it is that it’s a GUI based tool that can be used with python shell. We can add breakpoints in IDE and run our code in shell. Whenever the code flow hits the breakpoint , you can continue debugging in the native GUI of the editor. Having GUI debugger is very helpful in getting to know the code, as you can see your code getting executed line by line and it’s super easy to understand if you're already familiar with the IDE.

Get Karan Churi’s stories in your inbox

Join Medium for free to get updates from this writer.

To get started with debugpy you need to install the library in your environment.

pip install debugpy

Below is the setup for Visual studio code IDE

  1. Install python extension for vscode to get necessary python tools

2. Now instead of running regular ipython shell use below

python3 -m debugpy --listen 5678 ~/.pyenv/versions/3.8.2/bin/ipython

Change above path as per your ipython environment.

3. Run with below instructions

Press enter or click to view image in full size

Once you click on remote attach, use the default host as localhost and port as 5678

4. Add breakpoints anywhere in code where you want to debug. And finally execute your code in the shell, if the code flow check into the breakpoint, it will pause the execution and from then onwards you can debug your code with vscode built in debugger UI.

Press enter or click to view image in full size

Thanks for reading this far ✌🏼, let me know in comments if you find it useful or have some other techniques that makes debugging easy

--

--