【问题标题】:Is is possible for a python script to know whether it is run with the interactive option '-i'? [duplicate]python脚本是否可以知道它是否使用交互式选项'-i'运行? [复制]
【发布时间】:2012-08-11 15:55:45
【问题描述】:

可能重复:
tell whether python is in -i mode
Tell if python is in interactive mode

有没有办法使用交互式选项 -i 来检查 python 脚本是否已运行?

例如

if interactive_mode:
    print 'I am in interactive mode!'
else:
    print 'I am in batch mode!'

然后调用

python hello_world.py
I am in batch mode!

python -i hello_world.py
>> I am in interactive mode!

【问题讨论】:

    标签: python


    【解决方案1】:
    import sys
    if sys.flags.interactive:
        print 'I am in interactive mode!'
    else:
        print 'I am in batch mode!'
    

    【讨论】:

      猜你喜欢
      • 2012-07-04
      • 2012-01-11
      • 2019-05-23
      • 2019-09-03
      • 2011-06-05
      • 2013-01-01
      • 2017-10-31
      • 2023-03-17
      相关资源
      最近更新 更多