Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; ';-)': 0.03; 'else:': 0.03; 'subject:IDLE': 0.04; 'importing': 0.05; 'interpreter': 0.05; 'detect': 0.07; 'linux,': 0.07; 'sys': 0.07; 'tool,': 0.07; 'advance': 0.07; '22,': 0.09; 'any)': 0.09; 'loaded,': 0.09; 'solution,': 0.09; 'subject:How': 0.10; 'cc:addr:python-list': 0.11; 'python': 0.11; 'suggest': 0.14; '(apologies': 0.16; '-tkc': 0.16; '49,': 0.16; '55,': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'ideally,': 0.16; 'idle.': 0.16; 'idlelib': 0.16; 'inaccurate': 0.16; 'spyder,': 0.16; 'subject:under': 0.16; 'sys.modules': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'usenet': 0.16; 'wrote:': 0.18; 'pieces': 0.19; 'fit': 0.20; 'seems': 0.21; 'code,': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'entries': 0.24; 'versions': 0.24; 'looks': 0.24; 'environment': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'least': 0.26; 'header:In- Reply-To:1': 0.27; 'installed': 0.27; 'external': 0.29; 'am,': 0.29; "doesn't": 0.30; "i'm": 0.30; 'bunch': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'this.': 0.32; "we're": 0.32; 'open': 0.33; 'linux': 0.33; 'running': 0.33; 'fri,': 0.33; "i'd": 0.34; 'could': 0.34; 'connection': 0.35; 'test': 0.35; 'but': 0.35; 'there': 0.35; '2.6': 0.36; 'idle': 0.36; 'charset:us-ascii': 0.36; 'subject:?': 0.36; 'example,': 0.37; 'sure': 0.39; 'dave': 0.60; 'manually': 0.60; 'tell': 0.60; "you're": 0.61; 'such': 0.63; 'skip:n 10': 0.64; 'between': 0.67; 'received:50.22': 0.84; 'subject:tell': 0.84; 'angel': 0.91; '2013': 0.98 Date: Fri, 5 Apr 2013 09:33:42 -0500 From: Tim Chase To: Steven D'Aprano Subject: Re: How do I tell if I'm running under IDLE? In-Reply-To: <515ed3a7$0$29995$c3e8da3$5496439d@news.astraweb.com> References: <515e99c8$0$30001$c3e8da3$5496439d@news.astraweb.com> <515ed3a7$0$29995$c3e8da3$5496439d@news.astraweb.com> X-Mailer: Claws Mail 3.7.6 (GTK+ 2.20.1; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 56 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1365172337 news.xs4all.nl 6856 [2001:888:2000:d::a6]:37475 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:42826 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