Is it possible to debug a method called from the interactive window in PTVS?
Asked Answered
G

1

1

When I'm developing in Python I often want to debug a specific method, in which case it makes sense to call the method from the interactive console or debug interactive console. However, when a method is called from the interactive windows in PTVS, it doesn't stop at the break points in said method.

If it's possible, please tell me how to do it. If not, I would like to request this feature, and also to know if there is any quicker way to debug a specific method than calling it from the main script.

I'm using PTVS 2.0 RC in Visual Studio 2013 Ultimate

Grano answered 25/11, 2013 at 16:49 Comment(0)
M
2

When using the regular (non-debug) Python Interactive window, you can actually attach VS to the python.exe process that it is running by using Debug -> Attach to Process. Once that is done, if the interactive window does something to e.g. hit a breakpoint, the debugger will hit on that breakpoint.

The tricky part is loading the code from a file in such a way that breakpoints are resolved. In particular, $load REPL command will not work because it just reads the file and evals it in the REPL line by line, without preserving the original file context. What you need is to load your script using Python facilities - e.g. import, or open+exec.

There are also some gotchas there - e.g. the REPL window will become unresponsive whenever you are paused on a breakpoint.

Malik answered 25/11, 2013 at 21:46 Comment(2)
Ah I see, that's a nice trick, thanks. It's a shame the REPL becomes unresponsive though. It would be really nice if it were possible to do the following: While stopped at a breakpoint in method a, call method b from the debug interactive window, and have the debugger stop at any breakpoints in method b, and then call method c from the debug interactive window in method b's context. This is how the debugger works in Matlab, and I've gotten used to using these kind of "nested breakpoints" when developing.Grano
I see that after attaching the regular interactive window to python.exe and calling method a, stopping at a breakpoint does cause the REPL to become unresponsive, but we can then switch to the debug interactive window and play with the local variables which is very cool.Grano

© 2022 - 2025 — McMap. All rights reserved.