How to tell if interpreter is in interactive mode

Alex Martelli aleaxit at yahoo.com
Mon Jun 18 11:39:22 EDT 2001


[posted and mailed]

"John Copella" <jcopella at cfl.rr.com> wrote in message
news:irMV6.446535$o9.72583096 at typhoon.tampabay.rr.com...
> Thanks for your suggestions folks.  Basically what I have is a module that
I
> want to have print some identifying information when it is imported, but
> only during interactive sessions -- if the user imports the module from a
> script I don't want to clutter the tty with this stuff when the script
runs.
> I hadn't thought about the IDE aspect as I use the command line for my
work,
> but it's something to consider.
>
> Either way, I'm a little surprised this isn't easier to determine.

It IS pretty easy -- sorry, hadn't noticed your question earlier!
Hoping the answer is still of interest despite the 5 days since
you asked...:

In module sys, an attribute named ps1 exists during interactive
interpreter sessions.  IDE's such as IDLE and PythonWin also set
it (it's normally the string ">>> ").  This attribute is *NOT* set
if the interpreter is NOT interactive.

Assuming other IDE's etc also follow this sensible convention
(can anybody using other IDE's pls check...?), then:

def aminteractive():
    import sys
    return hasattr(sys, 'ps1')


Alex






More information about the Python-list mailing list