如何在控制台或 shell 中查看 python 是否为 运行

How to view if python is running in console or in shell

所以我目前正在编写一个 python 程序,它利用

getpass.getpass()

函数,但是这在 IDLE shell 中不起作用,仅在 console/terminal 中起作用,所以无论如何可以查看我是否 运行 python 中的程序shell 或 console/terminal。如果是这样的话,我可以做类似的事情:

if python == 'console':
    a = getpass.getpass()

elif python == 'idle':
    a = input()

如果可以的话,这将非常有用,所以希望你可以。

import sys; 'idlelib' in sys.modules 在空闲时 运行 为真,而在终端 运行 时不为真(除非你的程序导入它)。