【发布时间】:2011-01-22 07:40:43
【问题描述】:
在 Python 脚本中,有没有办法判断解释器是否处于交互模式?这很有用,例如,当您运行交互式 Python 会话并导入模块时,会执行稍微不同的代码(例如,关闭日志记录)。
我查看了tell whether python is in -i mode 并尝试了那里的代码,但是,该函数仅在使用 -i 标志调用 Python 时返回 true,而不是在用于调用交互模式的命令为 python 且没有论据。
我的意思是这样的:
if __name__=="__main__":
#do stuff
elif __pythonIsInteractive__:
#do other stuff
else:
exit()
【问题讨论】:
-
考虑使用
logging模块登录库,以便用户可以通过共享配置对其进行控制。
标签: python interactive python-2.5 python-2.x