Tell if Python is in interactive mode

后端 未结 7 1017
北荒
北荒 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:56

    From TFM: If no interface option is given, -i is implied, sys.argv[0] is an empty string ("") and the current directory will be added to the start of sys.path.

    If the user invoked the interpreter with python and no arguments, as you mentioned, you could test this with if sys.argv[0] == ''. This also returns true if started with python -i, but according to the docs, they're functionally the same.

提交回复
热议问题