Detecting interactive mode reliably

Matthew Barnes matthew at barnes.net
Mon Sep 8 11:45:51 EDT 2003


Surely this has been asked before, but I've not yet found the answer
on Google Groups or the Python FAQ.

Is there a method for reliably detecting whether the interpreter is
running in interactive mode?  The best solution I've come up with so
far is to check sys.argv[0] for an empty string or '-c', but I'm not
sure whether that works for all the various Python IDEs out there.

Reason for the question is that I'd like a module I'm writing to run
some extra initialization steps when the module is imported from an
interactive interpreter.

So something like:

if some_reliable_interactive_mode_test():
    initialize_for_interactive_use()

Matthew Barnes




More information about the Python-list mailing list