Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to debug a subprocess using pydev?

I'm using Eclipse / PyDev trying to find a way to debug code that uses subprocess.Popen to create a child process: I want to be able to debug the child process that is created. The problem is that I cannot find a way to debug accross process boundaries, and I'm guessing that it is actually not possible. Still, you never know until you ask, and so that I am doing!

A bit of background: I have a complex build process driven by Waf which invokes our unit tests by calling out to nose as required: I want to hook into these processes to debug unit test failures. I know I could try to run nose directly but the problem is that the environment I have to configure for our modules to load correctly is fairly complex and I don't want to duplicate the code to do that if I can avoid it.

I'm aware of the remote debugging mode but thats pretty inconvenient because I have to manually invoke the debugger in the remote process. If anyone knows a way to do what I'm trying to do it would be much appreciated.

like image 309
jkp Avatar asked Oct 26 '09 13:10

jkp


People also ask

How do I debug in Pydev?

Now, to debug that file, you can use Shift+F9 (the editor must be focused). NOTE: if you want to re-run the last executed file, you can use F11 to debug it (if you haven't configured F11 to always launch the last launch in debug mode, make sure you read the Getting Started on Running a program).

How to debug a Python file in VS code?

If you're only interested in debugging a Python script, the simplest way is to select the down-arrow next to the run button on the editor and select Debug Python File in Terminal.

How to debug Python script?

Starting Python Debugger To start debugging within the program just insert import pdb, pdb. set_trace() commands. Run your script normally, and execution will stop where we have introduced a breakpoint. So basically we are hard coding a breakpoint on a line below where we call set_trace().


2 Answers

I does not seem PyDev can do it (neither can PyDbg and WinDbg), but it looks like gdb can: http://wiki.python.org/moin/DebuggingWithGdb.

like image 195
Raphaël Saint-Pierre Avatar answered Nov 07 '22 19:11

Raphaël Saint-Pierre


I've found something of a workaround that might work for you.

Like you, I first found the remote debugging option of manually inserting calls to pydevd.settrace() at desired breakpoints. But I also noticed that subsequent PyDev breakpoints (i.e. those created by clicking in the left margin) were obeyed. So it seems that you just need the first explicit settrace call to establish the remote debugging session for the process, and afterwards just use the normal debugger breakpoints.

Moreover, you can modify the settrace call so it doesn't actually suspend the process:

import pydevd
pydevd.settrace(suspend=False)

So insert the above code somewhere early in the initialization of the subprocess and you should be good. Still a bit of a hack, but it's definitely better than the manual method.

like image 3
pimlottc Avatar answered Nov 07 '22 19:11

pimlottc