python idle解释器的命令提示符是_找出python脚本是否在IDLE或终端/命令提示符中运行...

Is there a way to find out if the python script is running in the IDLE interpreter or the terminal?

Works cross-platform if possible, or if needed a different way for each platform.

Work with Python 2 and Python 3 if possible, or if needed a different way for each version.

The only way I could think of is checking the processes running for IDLE but I don't know how to do that right.

If IDLE is open for another script and my script is running in the terminal, a process check would return true even if my script is not running in the IDLE.

My script needs to run differently depending on if it is running in IDLE or a terminal.

解决方案

This seems to work on Python3/Linux

import sys

print("idlelib" in sys.modules)

If will return True if the script is run from Idle, False otherwise. Please test for other combination of Python/OS !

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值