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

  • Thread starter Steven D'Aprano
  • Start date
S

Steven D'Aprano

(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.
 
D

Dave Angel

(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.
 
S

Steven D'Aprano

(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.
 
T

Tim Chase

(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
 
T

Terry Jan Reedy

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.
 
M

Mark Janssen

(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
 
D

Dave Angel

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top