How to tell if interpreter is in interactive mode

Alex Martelli aleaxit at yahoo.com
Fri Jun 29 09:33:00 CEST 2001


"Levente Sandor" <sandorlevi at yahoo.com> wrote in message
news:3b33133a.13483590 at news.unbc.ca...
    ...
> >> In module sys, an attribute named ps1 exists during interactive
> >> interpreter sessions.  IDE's such as IDLE and PythonWin also set
    ...
> >The only catch is that this attribute is not yet
> >defined when sitecustomize is imported.
    ...
> Try this:
>
> import sys
>
> def IsInteractive():
>     try :
>         if sys.ps1: return 1
>     except: pass
>     return 0
>
> print IsInteractive()

Futile: as Thomas indicated, this does not work if you place it
in sitecustomize.py.  sitecustomize is imported very early, before
such attributes of module sys as ps1 and argv are set up.

One possible workaround in some cases might be...: as early as
the import of site, and sitecustomize, it's not known yet if
the session is going to be interactive.  But by the same token,
if it IS going to be interactive, then Python has not yet looked
at the PYTHONSTARTUP environment variable to determine whether
to use a startup file.  Therefore, sitecustomize.py MAY modify
os.environ["PYTHONSTARTUP"] *AND* thus affect Python's behavior
at the later stage of interactive/non-interactive determination.

If your desired site-customization behavior, that differs for
interactive and non-interactive sessions, can be effected as:
    -- prepare things in sitecustomize.py assuming the session
        is *NOT* interactive,
    -- and later if it turns out to BE interactive, fix things
        in sitecustomizeinteractive.py,
then you can do it.  For example, in sitecustomize.py:

import os
os._save_old_startup = os.environ.get("PYTHONSTARTUP")
os.environ["PYTHONSTARTUP"] = "sitecustomizeinteractive.py"
# proceed with other settings assuming non-interactive

and in sitecustomizeinteractive.py:

import os
# finish up settings for interactive session, then:
del os.environ["PYTHONSTARTUP"]
old_startup = os._save_old_startup
del os._save_old_startup
if old_startup:
    os.environ["PYTHONSTARTUP"] = old_startup
    try: execfile(old_startup)
    except: pass

This is untested (except I did check that, in Python 2.1
at least, setting os.environ["PYTHONSTARTUP"] at the time
of sitecustomize does work as intended) and will no doubt
need tweaking (e.g., use full path for the "customize
when interactive" script), but it might be a start.


I believe a more solid solution would need reworking a
bit the architecture of Py_Main in Modules\main.c, so
that the various parameters that Py_Main uses to decide
whether to use PYTHONSTARTUP (command==NULL and
filename==NULL and stdin_is_interactive, presumably)
are also collected into one flag and made available
already at PyInitialize time.  But there are some
decisions to be taken -- for example, when the -i
flag ("inspect", but I've always thought of it as
"interactive":-) is used, Python does give an interactive
prompt after running the specified script, BUT it's not
"interactive" in the sense that it doesn't source
PYTHONSTARTUP earlier.  So presumably this flag may
need to be exposed separately, too, in order to let
some sitecustomize.py distinguish between "interactive
in the normal, full sense" and "interactive only because
-i was explicitly specified".  And I'm not sure how to
generalize that to other cases, where Python is NOT
started by means of Py_Main...


Alex






More information about the Python-list mailing list