Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #42801 > unrolled thread
| Started by | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| First post | 2013-04-05 20:30 +1100 |
| Last post | 2013-04-06 20:43 -0400 |
| Articles | 7 — 5 participants |
Back to article view | Back to comp.lang.python
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
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-04-05 20:30 +1100 |
| Subject | How do I tell if I'm running under IDLE? |
| Message-ID | <515e99c8$0$30001$c3e8da3$5496439d@news.astraweb.com> |
(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
[toc] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-04-05 07:04 -0400 |
| Message-ID | <mailman.129.1365159902.3114.python-list@python.org> |
| In reply to | #42801 |
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.
>
> 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.
>
>
>
Are you open to OS-specific techniques? For example, on Linux, you
could learn things from ps aux.
--
DaveA
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-04-05 13:37 +0000 |
| Message-ID | <515ed3a7$0$29995$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #42803 |
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. -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2013-04-05 09:33 -0500 |
| Message-ID | <mailman.142.1365172337.3114.python-list@python.org> |
| In reply to | #42819 |
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
[toc] | [prev] | [next] | [standalone]
| From | Terry Jan Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2013-04-06 19:23 -0400 |
| Message-ID | <mailman.217.1365290603.3114.python-list@python.org> |
| In reply to | #42801 |
On 4/5/2013 5:30 AM, Steven D'Aprano wrote:
> I'm looking for an official way to tell what interpreter (if any) is
> running, or at least a not-too-horrible unofficial way.
The interpreters distributed by PSF identify themselves on startup, as
least in interactive mode, by displaying a line like "Python 3.3.0+
(default, Mar 30 2013, 17:36:11) [MSC v.1600 32 bit (Intel)] on win32"
The version available as sys.version, sys.hexversion, and
sys._version(). The platform is available as sys.platform. The compiler
and compile date are not officially available.
But you seem not to be actually asking 'what interpreter is running' but
'who started the interpreter' and 'is the interpreter i/o connected to a
text-mode OS console or to a gui program, such as IDLE, more or less
simulating an OS console.
There is no official way because it should not matter to a running
program. It is the goal of at least some IDLE developers to make the
connection to the IDLE shell as transparent as possible to Python code,
so that it matters as little as possible*. This is one reason for the
shift from running user code in the same process and interpreter
instance as Idle to running user code in a separate process with its own
interpreter instance.
* There are current tracker issues about making this true. We really do
not want people to have to write conditional code, unless it is to work
around a console problem if Idle is not being used ;-).
If you have an issue not already covered on the tracker, I am curious
what it is.
> 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 is based on the fact that if running in the same process, there are
a lot more imported modules and if running in a separate process, there
are at least a few more imported modules to set up the rpc connection. I
do not know that '20' is particularly reliable, even on startup.
> This one is better, but still not exactly what I consider great:
Why not, if is works for all Idle versions so far? The irreducible
effect of any non-OS-console environment is to override the normal i/o.
> sys.stdin.__class__.__module__.startswith('idlelib')
'idlelib' in sys.stdin.__class__.__module__' is possibly more future-proof.
> Ideally, I'd like to detect any arbitrary environment such as Spyder,
> IPython, BPython, etc., but will settle for just IDLE.
I expect that looking as sys.stdin in someway should work for all.
--
Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | Mark Janssen <dreamingforward@gmail.com> |
|---|---|
| Date | 2013-04-06 17:35 -0700 |
| Message-ID | <mailman.218.1365294934.3114.python-list@python.org> |
| In reply to | #42801 |
> (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. I was going to work on this IDLE TODO myself, but haven't got around to it. I think the best way is to use the tempfile module (to be clear this is a fix within IDLE itself). This is the whole procedure that should be necessary: 1) At IDLE startup check to see if there is an existing idle tempfile and delete it, if so. 2) Create an IDLE temp file. 3) At IDLE shutdown, delete prior tempfile. Now if anyone wants to see if IDLE is running, they only need to check to see if the tempfile exists. Hope that helps. If you implement it, you can take the item off the IDLE todo list too. MarkJ Tacoma, Washington
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-04-06 20:43 -0400 |
| Message-ID | <mailman.219.1365295417.3114.python-list@python.org> |
| In reply to | #42801 |
On 04/06/2013 08:35 PM, Mark Janssen 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. > > I was going to work on this IDLE TODO myself, but haven't got around > to it. I think the best way is to use the tempfile module (to be > clear this is a fix within IDLE itself). > > This is the whole procedure that should be necessary: > > 1) At IDLE startup check to see if there is an existing idle tempfile > and delete it, if so. > 2) Create an IDLE temp file. > 3) At IDLE shutdown, delete prior tempfile. > > Now if anyone wants to see if IDLE is running, they only need to check > to see if the tempfile exists. > > Hope that helps. If you implement it, you can take the item off the > IDLE todo list too. > Are you assuming then that there can be only one instance of IDLE ? And that if a separate Python program is run, we should pretend that it's running under IDLE? -- DaveA
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web