RobotLocomotion / director

A robotics interface and visualization framework, with extensive applications for working with http://drake.mit.edu
BSD 3-Clause "New" or "Revised" License
179 stars 86 forks source link

Debugging python code run by startup #632

Closed heuristicus closed 3 years ago

heuristicus commented 3 years ago

I am trying to debug some python code which is run in a customised startup.py that I am using. I would like to use a debugger such as pdb to do this but have so far been unable to make it work.

I have tried adding

import pdb
pdb.set_trace()

To the startup script, but while I see some output from pdb I am unable to type any commands. I think this is likely caused by stdin/stdout not being linked to the terminal because of the way that the python file is executed.

Usually I would attach to a python process directly but again because of how PythonQt runs the python code, it is all contained within the single director process.

Is there any way I can get a debugger to attach to the python code?

patmarion commented 3 years ago

Hi,

If the main window is open and responsive then you can use the built in Python console (press F8 to open it) to enter text to interact with pdb. If you have set the trace before the application has finished initializing then you will have to use the terminal. You can restore the stdin with this code before calling set_trace:

import sys
sys.stdin = sys.__stdin__
heuristicus commented 3 years ago

Thanks, that was what I needed.