How to tell if interpreter is in interactive mode

John Copella jcopella at cfl.rr.com
Wed Jun 13 12:22:38 EDT 2001


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.

Michael Hudson <mwh at python.net> wrote in message
news:m3iti0wl90.fsf at atrus.jesus.cam.ac.uk...
> "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