[Python-ideas] Suggestion for 2.7/3.[01] - sys.isinteractive()

skip at pobox.com skip at pobox.com
Sat Nov 15 01:15:59 CET 2008

We have a special exception handling hook written for the application
platform we developed at work.  Mostly it makes sure any exceptions raised
get their tracebacks logged before going belly up.  But it also makes sure
the program does go belly up.

When using bits of this platform in an interactive session it can be
annoying (to say the least) for the interpreter to exit when you make a
spelling error.  Consequently I wanted something like:

    if we are not running interactively:
        sys.excepthook = exception_handler

The only problem was to decide what the proper python-speak was for "we are
not running interactively".  It seemed to me that sys.argv[0] was the empty
string when running interactively but that seemed fragile so I decided to
ask on comp.lang.python.  Peter Otten responded with

    hasattr(sys, "ps1")

and a link to the sys module doc section where the presence of sys.ps1 only
in interacive mode is documented.

So now I know the proper spelling in python-speak, but that seems like a
very non-obvious way to do that (and a bit fragile since it's possible, if
not exactly reasonable to "del sys.ps1".  It seems to me that it would be
preferable if sys had an "interactive" attribute or an "isinteractive"
method.  Maybe in 2.7 and 3.0 or 3.1?  Am I way off base?  I realize that
would present two ways to do it, but in my mind one would be obvious, the
other not.



More information about the Python-ideas mailing list