Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Python, how do I debug with an interactive command line (and visual breakpoints?)

I've recently moved from Matlab to Python. Python is a much better language (from the point of view of a computer scientist), but Python IDEs all seem to lack one important thing:

A proper interactive debugger.

I'm looking for:

  • The ability to set breakpoints graphically by clicking next to a line of code in the editor.

  • The ability to run ANY CODE while stopped in the debugger, including calling functions from my code, showing new windows, playing audio, etc.

  • When an error occurs, the debugger should automatically open an interactive console at the error line.

  • Once done with the interactive console, you can resume normal execution.

Matlab has all these features and they work incredibly well, but I can't find them anywhere in Python tools.

I've tried:

  • PyCharm: the interactive console is clunky, often fails to appear, and crashes all the time (I've tried several different versions and OSs).

  • IPython: can't set breakpoints -Launching a Python console programatically: you have to stop your code, insert an extra line of code, and run again from the beginning to do this. Plus, you can't access functions already imported without re-importing them.

Being able to debug and fix problems THE FIRST TIME THEY APPEAR is very important to me, as I work in programs that often take dozens of minutes to re-run (computational neuroscience).

CONCLUSION: there is NO way to do all of these in Python at the moment. Let us hope that PyLab development accelerates.

like image 509
Louise Avatar asked Dec 12 '13 15:12

Louise


People also ask

How do I debug a visual code in Python?

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. By default, a Python debugger is not installed in VSCode.

How do you debug Python command line arguments?

Go to your project properties, either by right-clicking on the project and picking "Properties" or by picking Properties from the Project menu. Click on Debug, then enter your arguments into the "Script Arguments" field. Save.

How do I debug a command line?

Specifies command line information required by the file you want to test. After debug starts, type "?" to display a list of debugging commands. To get out of debugging mode, you need to type "Q" and press Enter . To execute the debug routine, you need to type "G" and press Enter .


1 Answers

At the top of your code, write

import pdb

And within your code, use the following statement wherever you want to debug.

pdb.set_trace()

You will have an interactive shell thus, whenever the set_trace() statement is met.

You can then use step(s), next(n), continue(c) and so on to check the execution flow, and print values of variables like print var

For more details on pdb, refer here

like image 170
Anshul Goyal Avatar answered Oct 05 '22 03:10

Anshul Goyal