How do I tell if I'm running under IDLE?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Apr 5 05:30:47 EDT 2013


(Apologies in advance if you get multiple copies of this. My Usenet
connection seems to be having a conniption fit at the moment.)

I'm looking for an official way to tell what interpreter (if any) is
running, or at least a not-too-horrible unofficial way.

Googling comes up with a number of hacks for detecting IDLE. Some of them
are terrible. For example, I can't believe that somebody actually
suggested this:

if len(sys.modules) > 20:
    print "running under IDLE"


This one is better, but still not exactly what I consider great:

sys.stdin.__class__.__module__.startswith('idlelib')



Ideally, I'd like to detect any arbitrary environment such as Spyder,
IPython, BPython, etc., but will settle for just IDLE.



-- 
Steven




More information about the Python-list mailing list