I posted the following a while back. I think this is what you are
looking for.
This can be done fairly easily by creating a module (lets call it
interactive) with the following code in it.
-----------
import sys,os
def debug_exception (type, value, traceback):
# Restore redirected standard I/O
sys.stdin = sys.__stdin__
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
# Kick the interpreter into interactive mode and call the original
# exception handler.
os.environ['PYTHONINSPECT'] = '1'
sys.__excepthoo k__(type, value, traceback)
sys.excepthook = debug_exception
-----------
Now if you import this module and raise an unhandled exception, you will
be in interactive mode. In other words, I think the following script
does what you are asking for.
-----------
import interactive
raise RuntimeError('I nteractive Mode')
-----------
This also has the advantage that if there are no unhandled exceptions in
your script, the script runs and terminates normally.
Enjoy,
Ray Buvel
Joe wrote:
Hi Pierre,
Thanks for the reply, but I am not on Unix and it even if I was that
solution it does not achieve the desired results.
I want the script to decide whether to fall back to the interactive prompt.
You solution makes it ALWAYS fall back to the interactive prompt.
I want to do something like:
try:
# execute code
except MyExceptionOccu rred, except_msg:
# fall to interactive prompt.
In other words I want to have my program to determine whether the
interactive prompt to be displayed or not.
Thanks!
"Pierre Barbier de Reuille" <pi************ @cirad.fr> wrote in message
news:42******** *************** @news.free.fr.. .
Very simple is you're on UNIX ...
You juste have to put at the beginnin of your file :
#!/usr/bin/python -i
And it juste does what you want :)
Pierre
Joe a écrit :
When you run "python -i scriptname.py" after the script completes you
left at the interactive command prompt.
Is there a way to have this occur from a running program?
In other words can I just run scriptname.py (NOT python -i scriptname.py)
and inside of scriptname.py I decide that I want to fall back to the
interactiv e prompt?
I've searched and so far the only thing I've come up with is to use pdb,
but that is not exactly the same as the interactive prompt.
Is there any way to do it that I have missed?
Thanks.