找出python脚本是否在IDLE或终端/命令提示符中运行 [英] Find out if a python script is running in IDLE or terminal/command prompt
问题描述
是否有办法找出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.
如果可能,使用Python 2和Python 3,或者如果需要,则使用不同的方式使用每个版本。
Work with Python 2 and Python 3 if possible, or if needed a different way for each version.
我唯一想到的方法是检查IDLE运行的进程,但我不知道该怎么做。
The only way I could think of is checking the processes running for IDLE but I don't know how to do that right.
如果IDLE是打开另一个脚本,并且我的脚本正在终端中运行,即使我的脚本不在IDLE中运行,进程检查也将返回true。
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.
我的脚本需要运行
推荐答案
该似乎可以进行操作Python3 / Linux
This seems to work on Python3/Linux
import sys
print("idlelib" in sys.modules)
如果脚本是从Idle运行的,则返回 True
, F否则
。请测试其他Python / OS组合!
If will return True
if the script is run from Idle, False
otherwise. Please test for other combination of Python/OS !
这篇关于找出python脚本是否在IDLE或终端/命令提示符中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!