Find out if a python script is running in IDLE or terminal/command prompt

强颜欢笑 提交于 2019-12-12 10:49:07

问题


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.


回答1:


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 !



来源:https://stackoverflow.com/questions/17133769/find-out-if-a-python-script-is-running-in-idle-or-terminal-command-prompt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!