我可以使用什么代码来检查脚本是否在 IDLE 中运行?

发布于 2024-09-13 07:27:23 字数 242 浏览 2 评论 0原文

正如标题所说。我想编写一个行为不同的脚本,具体取决于它是在控制台窗口内运行还是在空闲状态下运行。是否有一个仅在空闲状态下运行时才存在的对象可供我检查?环境变量?

我在 Windows 上使用 Python 2.6.5 和 2.7。

编辑:

到目前为止给出的答案有效。但我正在寻找一种官方的方法来做到这一点,或者看起来不像黑客的方法。如果有人提出一个,我会接受它作为答案。否则,几天后,我会接受最早的答案。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

划一舟意中人 2024-09-20 07:27:23

我更愿意这样做:

import sys
print('Running IDLE' if 'idlelib.run' in sys.modules else 'Out of IDLE')

I would prefer to do:

import sys
print('Running IDLE' if 'idlelib.run' in sys.modules else 'Out of IDLE')
指尖上的星空 2024-09-20 07:27:23

Google 找到了我 this 2003 年的论坛帖子。使用 Python 3.1(适用于 win32)及其附带的 IDLE 版本,命令行中的 len(sys.modules) os 为 47,但 IDLE 中为 122壳。

但为什么你还需要关心呢? Tkinter 代码在使用 IDLE 运行时有一些烦恼(因为后者使用 tkinter 本身),但除此之外,我认为我可以安全地假设您不必关心。

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.

对岸观火 2024-09-20 07:27:23

我建议将所有代码打包在一个函数中(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()]

这样 tkinter 应用程序就可以进行检查:

if not RunningIntoPythonIDLE():
    root.mainloop()

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()
瞄了个咪的 2024-09-20 07:27:23

我有点晚了,但由于 IDLE 用自定义对象替换了标准流(并且已记录),可以检查这些以确定脚本是否在 IDLE 中运行:

import sys

def in_idle():
    try:
        return sys.stdin.__module__.startswith('idlelib')
    except AttributeError:
        return True

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
☆獨立☆ 2024-09-20 07:27:23

我的建议是获取所有正在运行的帧的列表,并检查主要的 Idle 方法是否在其中。

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()])

Frames 函数生成在声明时运行的帧,因此您可以检查此处是否空闲。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文