How to tell if interpreter is in interactive mode

Levente Sandor sandorlevi at
Fri Jun 22 11:42:36 CEST 2001

On Fri, 22 Jun 2001 02:25:59 -0700, "Thomas Heller"
<thomas.heller at> wrote:

>"Alex Martelli" <aleaxit at> wrote in message news:9gl7ba07u8 at
>[John Copella asked how to tell if interpreter is in interactive mode]
>> 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.
>The only catch is that this attribute is not yet
>defined when sitecustomize is imported.
>Any other solutions?

Try this:

import sys

def IsInteractive():
    try :
        if sys.ps1: return 1
    except: pass
    return 0

print IsInteractive()


More information about the Python-list mailing list