Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #42826

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

Date 2013-04-05 09:33 -0500
From Tim Chase <python.list@tim.thechases.com>
Subject Re: How do I tell if I'm running under IDLE?
References <515e99c8$0$30001$c3e8da3$5496439d@news.astraweb.com> <mailman.129.1365159902.3114.python-list@python.org> <515ed3a7$0$29995$c3e8da3$5496439d@news.astraweb.com>
Newsgroups comp.lang.python
Message-ID <mailman.142.1365172337.3114.python-list@python.org> (permalink)

Show all headers | View raw


On 2013-04-05 13:37, Steven D'Aprano wrote:
> On Fri, 05 Apr 2013 07:04:35 -0400, Dave Angel wrote:
> 
> > On 04/05/2013 05:30 AM, Steven D'Aprano wrote:
> >> (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.
> [...]
> >> Ideally, I'd like to detect any arbitrary environment such as
> >> Spyder, IPython, BPython, etc., but will settle for just IDLE.
> >
> > Are you open to OS-specific techniques?  For example, on Linux,
> > you could learn things from ps aux.
> 
> I'd prefer a pure-Python solution, but if the choice is between an 
> accurate solution using an external tool, and an inaccurate Python 
> heuristic, I'm willing to use external tools.

If you're looking for a heuristic, you might take a look at whether
there are bunch of entries in sys.modules that start with
"idlelib".  There seem to be a bunch when tested in the IDLE Python
shell. You could set some low-water mark to suggest that IDLE is
loaded, and take the count:

  import sys
  IDLE_LOW_WATER_MARK = min([
    49, # Python 2.4 on WinXP
    55, # Python 2.6 on Linux
    22, # Python 3.1 on Linux
    # I seem to recall you have oodles of other
    # versions installed that you can test
    # to find a good low-water mark
    ])
  idle_module_count = sum([
    1 for m in sys.modules
    if m.startswith("idlelib.") or m == "idlelib"
    ])
  print("Found %i idlelib module(s), hunting %i" %
    idle_module_count, IDLE_LOW_WATER_MARK)
  if idle_module_count >= IDLE_LOW_WATER_MARK:
    print("Hey, it looks like we're running in IDLE")
  else:
    print("I'm pretty sure we're not running in IDLE")

It doesn't stop you from manually importing IDLE_LOW_WATER_MARK
modules into your own code, but then you just get to keep the pieces
when it breaks that way ;-)

-tkc



Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How do I tell if I'm running under IDLE? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-05 20:30 +1100
  Re: How do I tell if I'm running under IDLE? Dave Angel <davea@davea.name> - 2013-04-05 07:04 -0400
    Re: How do I tell if I'm running under IDLE? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-05 13:37 +0000
      Re: How do I tell if I'm running under IDLE? Tim Chase <python.list@tim.thechases.com> - 2013-04-05 09:33 -0500
  Re: How do I tell if I'm running under IDLE? Terry Jan Reedy <tjreedy@udel.edu> - 2013-04-06 19:23 -0400
  Re: How do I tell if I'm running under IDLE? Mark Janssen <dreamingforward@gmail.com> - 2013-04-06 17:35 -0700
  Re: How do I tell if I'm running under IDLE? Dave Angel <davea@davea.name> - 2013-04-06 20:43 -0400

csiph-web