判断 Python 是否处于交互模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2356399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tell if Python is in interactive mode
提问by Chinmay Kanchi
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 session and import a module, slightly different code is executed (for example, logging is turned off).
在 Python 脚本中,有没有办法判断解释器是否处于交互模式?这将很有用,例如,当您运行交互式 Python 会话并导入模块时,执行的代码略有不同(例如,关闭日志记录)。
I've looked at tell whether python is in -i modeand tried the code there, however, that function only returns true if Python has been invoked with the -i flag and not when the command used to invoke interactive mode is python
with no arguments.
我查看了告诉 python 是否处于 -i 模式并在那里尝试了代码,但是,该函数仅在使用 -i 标志调用 Python 时才返回 true,而不是在用于调用交互模式的命令python
没有参数时返回.
What I mean is something like this:
我的意思是这样的:
if __name__=="__main__":
#do stuff
elif __pythonIsInteractive__:
#do other stuff
else:
exit()
回答by Ignacio Vazquez-Abrams
__main__.__file__
doesn't exist in the interactive interpreter:
__main__.__file__
交互式解释器中不存在:
import __main__ as main
print hasattr(main, '__file__')
This also goes for code run via python -c
, but not python -m
.
这也适用于通过 运行的代码python -c
,但不适用于python -m
。
回答by wersimmon
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.
来自TFM:如果没有给出接口选项,则隐含 -i,sys.argv[0] 是一个空字符串 (""),当前目录将被添加到 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.
如果用户在python
没有参数的情况下调用解释器,正如您所提到的,您可以使用if sys.argv[0] == ''
. 如果以 开头,这也会返回 true python -i
,但根据文档,它们在功能上是相同的。
回答by jdines
The following works both with and without the -i switch:
以下内容在使用和不使用 -i 开关时都适用:
#!/usr/bin/python
import sys
# Set the interpreter bool
try:
if sys.ps1: interpreter = True
except AttributeError:
interpreter = False
if sys.flags.interactive: interpreter = True
# Use the interpreter bool
if interpreter: print 'We are in the Interpreter'
else: print 'We are running from the command line'
回答by JAB
Here's something that would work. Put the following code snippet in a file, and assign the path to that file to the PYTHONSTARTUP
environment variable.
这是有用的东西。将以下代码片段放入文件中,并将该文件的路径分配给PYTHONSTARTUP
环境变量。
__pythonIsInteractive__ = None
And then you can use
然后你可以使用
if __name__=="__main__":
#do stuff
elif '__pythonIsInteractive__' in globals():
#do other stuff
else:
exit()
http://docs.python.org/tutorial/interpreter.html#the-interactive-startup-file
http://docs.python.org/tutorial/interpreter.html#the-interactive-startup-file