0

One of my favorite debug tools is a short piece of code which starts up a console. I sometimes debug by means of (roughly)

try: 
    #my code
    ...
except:
    launch_console()

However, this requires anticipating where the error will be. If I enclose the entire script in a try/except, I will not have access to any variables within the function I am debugging.

It would be magnificently convenient, if I could run the program and just automatically have launch_console() run in case of an error, without losing scope. I think spyder is supposed to have this functionality, but it does not work.

Wondering if there is some nice way to do this!

doublefelix
  • 952
  • 1
  • 9
  • 22
  • Related: [Starting python debugger automatically on error](https://stackoverflow.com/q/242485/11082165) – Brian61354270 Jul 05 '22 at 18:25
  • Also this one (hint: stack frames): https://stackoverflow.com/q/9059349/5378816 – VPfB Jul 05 '22 at 18:27
  • If this is possible, it's unlikely that you want to react to _any_ exception that's thrown. For example, responding to every `StopIteration` would get tiresome really quick – Brian61354270 Jul 05 '22 at 18:31
  • @Brian, that post seems nice, but I don't run the python command myself these days, and in Spyder it seems that you can only configure it on a per-file basis; I don't see myself realistically changing the settings each new file I make. – doublefelix Jul 05 '22 at 18:31
  • The comment from VPfB is also helpful, but the trouble is it would require remembering/writing a bunch of code from the sys library before accessing each variable name – doublefelix Jul 05 '22 at 18:34
  • The simple answer is that what you want doesn't really fit the Python model. – Barmar Jul 05 '22 at 18:52

0 Answers0