how to debug a python script

A user asked how to debug Python scripts. This is after said scripts have been made using the trace recorder (Tools/ Start Trace and Tools/ Stop Trace), and are being modified. Here is Cory’s reply:

The first thing to look at would be if there are any error messages. If there are, those might point the way to solving the bug. They will at least tell you the offending line in your Python script. If the problem is that there is no output, the next thing to look at would be to see if there are any calls to SaveData or SaveScreen. Make sure there is one, depending on whether you are trying to save data or images.

If the problem is that some properties are changed that should affect the generated image but they are not being shown, make sure to call Render or RenderAllViews.

If none of those work, then commenting things out until you get to a script that gives you an intermediate result that is expected is a good thing to do, at which point you can add things in incrementally until you identify the change that isn’t working. Cory.

Alan

It is also possible to use pdb in pvpython in order to use a true debugger.

This does not work correctly in the python shell, but could probably be improved.

So here is how to use pdb with the python shell.

Add the following code within your python script :

import pdb; pdb.set_trace()

Then run your scripts from the ParaView GUI. pdb will stop the execution and will be interactable using a dedicated input window. This could be improved in the future by integrating the input within the shell itself.

Added as a TIps and Tricks: How to debug a pvpython script