How to tell if interpreter is in interactive mode

Michael Hudson mwh at python.net
Wed Jun 13 09:40:59 EDT 2001


"John Copella" <jcopella at cfl.rr.com> writes:

> I have some code in a module that I need to run only when the
> interpreter is running interactively.  Is there any way to test for
> this at run time?  

There's a C global (Py_InteractiveFlag or somesuch), but I don't think
it's exposed to Python.

> I have looked at sys.stdin.isatty(), but this returns 1 when running
> non-interactively as well (unless you do something odd like redirect
> stdin to /dev/null).

Well, yes, seeing as sys.stdin is still connected to a tty...

Are you sure you want to do this?  If the user's using idle or my
pyrepl or something similar, then Python isn't running interactively
from its point of view, but is from the user's.

Cheers,
M.

-- 
    FORD:  Just pust the fish in your ear, come on, it's only a
           little one.
  ARTHUR:  Uuuuuuuuggh!
                    -- The Hitch-Hikers Guide to the Galaxy, Episode 1



More information about the Python-list mailing list