MATLAB has its weaknesses but one of the best things I like about MATLAB is the
keyboard command. It allows you to halt the program at a given point and gives you an interactive session that has access to all the functions and variables that are available to your script.
There are a few ways to do this in python. Of course, you could use gdb, just like you would to debug a C program. I know there are extra “plugins” that allow you access to Python specific debugging commands but I haven’t tried it out yet.
My preferred method is to use the Python Debugger or
pdb. The easiest way to do it is to launch your Python program as follows:
python -m pdb script.py
Then when your program hits an error, it will halt execution and give you an interactive console to debug the program right where the error was raised. It gives you access to all the variables and functions within the program at that particular point as well as a whole range of commands to help you with the debugging. Very useful especially when debugging useless error messages from third-party libraries.
There is another method that is a lot more similar to the MATLAB
keyboard function. Just import the
code module and the insert the command
code.interact(local=locals()) wherever you want to do the debugging. It gives you an interactive console to debug your Python program. The
local=locals() bit is required to give you access to all the local functions and variables.