LetMeSOThat4U
LetMeSOThat4U

Reputation: 6748

stopping execution of Python program on exception in ipython

I have a long running Python program that raises exception at some point. Is there some way to run this from ipython session and stop on the exception so I could examine the live data?

Upvotes: 2

Views: 1073

Answers (2)

Thomas
Thomas

Reputation: 6752

You may want ipython -i yourscript.py, which will execute your script in the interpreter environment. But this won't let you inspect the local environment where the exception happened, for example local variables within a function – you'll just be able to inspect globals. You probably want this instead:

In [1]: %run test.py
<exception occurs>
In [2]: %debug test.py

If you're not familiar with using PDB, check out some docs first.

Edit thanks to Thomas K

Upvotes: 3

cmd
cmd

Reputation: 5830

yes, depending on how you are setup. you can import your program and run it like any other module inside a try except block.

import yourprogram
try:
    yourprogram.main_function(args)
except:
    print "we blew up, investigate why"

If your program is not in a function you may need to put the try block around your import.

The problem with this approach is that the variables you are wanting to look at may be no longer in scope. I usually use print statements or log messages at various points to figure out what is not looking like I am expecting.

Upvotes: 0

Related Questions