Tell if Python is in interactive mode

后端 未结 7 1015
北荒
北荒 2020-11-28 10:03

In a Python script, is there any way to tell if the interpreter is in interactive mode? This would be useful so that, for instance, when you run an interactive Python sessio

7条回答
  •  孤独总比滥情好
    2020-11-28 10:45

    __main__.__file__ doesn't exist in the interactive interpreter:

    import __main__ as main
    print hasattr(main, '__file__')
    

    This also goes for code run via python -c, but not python -m.

提交回复
热议问题