What code can I use to check if script is running in IDLE?
Asked Answered
L

5

13

Just as the title says. I want to write a script that behaves differently depending on whether it's running inside a console window or in IDLE. Is there an object that exists only when running in IDLE that I can check for? An environment variable?

I'm using Python 2.6.5 and 2.7 on Windows.

Edit:

The answers given so far work. But I'm looking for an official way to do this, or one that doesn't look like a hack. If someone comes up with one, I'll accept that as the answer. Otherwise, in a few days, I'll accept the earliest answer.

Logarithm answered 7/8, 2010 at 18:13 Comment(0)
R
6

Google found me this forum post from 2003. With Python 3.1 (for win32) and the version of IDLE it comes with, len(sys.modules) os 47 in the command line but 122 in the IDLE shell.

But why do you need to care anyway? Tkinter code has some annoyances when run with IDLE (since the latter uses tkinter itself), but otherwise I think I'm safe to assume you shouldn't have to care.

Ricotta answered 7/8, 2010 at 18:25 Comment(6)
Hmm... sys.modules is a dict, with some keys that start with "idlelib.", so I could check for that. But it sounds like a hack, so I'd like to avoid it. They can even have different lengths (2.6.5:149, 2.7:152).Logarithm
Well, I guess this isn't possible without ugly hacks. That's the reason I ask why you think you need to...Ricotta
With the console, I want to be able to clear the screen, move the cursor, add color, use msvcrt.getch, etc. Most of those don't seem to work in IDLE, and some will even fail silently, so instead of letting that happen, I'd rather make it check if it's running in IDLE or not.Logarithm
Well, I fear you're out of luck here. Unless you want to hack around, that is. Sorry.Ricotta
One might care because multiprocessing examples tend to run poorly in IDLE.Match
getch does not work in IDLE, that would be a good time to know whether IDLE is being ran or not.Myer
K
15

I would prefer to do:

import sys
print('Running IDLE' if 'idlelib.run' in sys.modules else 'Out of IDLE')
Kraal answered 18/9, 2011 at 6:40 Comment(5)
This does not work with Python 2.7.2. Try to search for 'idlelib.__main__' insteadCompensable
It works in 2.7.3, just rerun it in IDLE command line and CMD in windowsXP and by running the script in both.Kraal
If you save this as a script and open it in IDLE from the right-click context menu, then it doesn't work. If you open IDLE first then open the script, it works. The method I suggested works in both cases [This is a known "feature" of IDLE: in the first case, it opens both the script and the interpreter in the same process]Compensable
'idlelib.__main__' doesn't work in (at least) python3.6. Just looking for 'idlelib' seems the most reliable.Writ
@Dhara: Your method does not work in Python 3.8.8 (and this answer does).Systole
R
6

Google found me this forum post from 2003. With Python 3.1 (for win32) and the version of IDLE it comes with, len(sys.modules) os 47 in the command line but 122 in the IDLE shell.

But why do you need to care anyway? Tkinter code has some annoyances when run with IDLE (since the latter uses tkinter itself), but otherwise I think I'm safe to assume you shouldn't have to care.

Ricotta answered 7/8, 2010 at 18:25 Comment(6)
Hmm... sys.modules is a dict, with some keys that start with "idlelib.", so I could check for that. But it sounds like a hack, so I'd like to avoid it. They can even have different lengths (2.6.5:149, 2.7:152).Logarithm
Well, I guess this isn't possible without ugly hacks. That's the reason I ask why you think you need to...Ricotta
With the console, I want to be able to clear the screen, move the cursor, add color, use msvcrt.getch, etc. Most of those don't seem to work in IDLE, and some will even fail silently, so instead of letting that happen, I'd rather make it check if it's running in IDLE or not.Logarithm
Well, I fear you're out of luck here. Unless you want to hack around, that is. Sorry.Ricotta
One might care because multiprocessing examples tend to run poorly in IDLE.Match
getch does not work in IDLE, that would be a good time to know whether IDLE is being ran or not.Myer
M
5

I suggest packing all the code in one function (Python 3):

def RunningIntoPythonIDLE():

    import idlelib.PyShell

    def frames(frame = sys._getframe()):
        _frame = frame
        while _frame :
            yield _frame
            _frame = _frame.f_back

    return idlelib.PyShell.main.__code__ in [frame.f_code for frame in frames()]

So tkinter apps can do its check:

if not RunningIntoPythonIDLE():
    root.mainloop()
Michaelson answered 7/7, 2011 at 10:41 Comment(0)
L
3

I'm a touch late, but since IDLE replaces the standard streams with custom objects (and that is documented), those can be checked to determine whether a script is running in IDLE:

import sys

def in_idle():
    try:
        return sys.stdin.__module__.startswith('idlelib')
    except AttributeError:
        return True
Lowlife answered 23/7, 2017 at 14:58 Comment(1)
If "it is happening in IDLE", sys.stdin.__module__.startswith('idlelib') returns True and if it is not, then there is an AttributeError, so in the except clause False should be returned, right?Banneret
H
1

My suggestion is to get list of all running frames and check if main Idle method would be in there.

def frames(frame = sys._getframe()):
    _frame = frame
    while _frame :
        yield _frame
        _frame = _frame.f_back
import idlelib.PyShell
print(idlelib.PyShell.main.func_code in [frame.f_code for frame in frames()])

the frames function generates frames running at moment of its declaration, so you can check if idle were here.

Horntail answered 7/8, 2010 at 20:10 Comment(1)
It works, but if it wasn't opened with a script, then it returns False. But there are other idlelib objects in it like <code object runcode at 00B9AE30, file "C:\Python26\lib\idlelib\run.py", line 287>. (tried with 2.6)Logarithm

© 2022 - 2024 — McMap. All rights reserved.