I need to determine whether the shell which invoked my Python script was in interactive mode or not. If it was in interactive mode, the program should pipe output to less(1) for easy reading. If not, it should simply print its output to stdout, to allow it to be piped away to a printer, file, or a different pager.
我需要确定调用我的Python脚本的shell是否处于交互模式。如果它处于交互模式,程序应将输出管道输出到较小(1)以便于阅读。如果没有,它应该只是将其输出打印到stdout,以允许它被传送到打印机,文件或不同的寻呼机。
In a shell script, I would have checked if the prompt variable $PS1 was defined, or looked for the -i option among the flags stored in the $- variable.
在shell脚本中,我会检查是否定义了提示变量$ PS1,或者在$ - 变量中存储的标志中查找-i选项。
What is the preferred method for testing interactivity from within Python?
在Python中测试交互性的首选方法是什么?
6 个解决方案
#1
22
This is often works well enough
这通常效果很好
import os, sys
if os.isatty(sys.stdout.fileno()):
...
#2
1
From this link you can use the same way and test if stdin is associated to a terminate(tty), you can do this using os.isatty(), example:
从这个链接你可以使用相同的方式并测试stdin是否与terminate(tty)相关联,你可以使用os.isatty()来做到这一点,例如:
>>> os.isatty(0)
True
N.B: From the same link this will fails when you invoke the command remotely via ssh, the solution given is to test if stdin is associated to a pipe.
N.B:从同一个链接,当你通过ssh远程调用命令时,这将失败,给出的解决方案是测试stdin是否与管道相关联。
#3
-1
if sys.flags.interactive:
#interactive
else:
#not interactive
http://docs.python.org/library/sys.html#sys.flags
http://docs.python.org/library/sys.html#sys.flags
#4
-1
why not symply (python3.X):
为什么不symply(python3.X):
import os
def is_interactive():
'check if current session is interactive'
try:
os.path.isdir(os.path.abspath(__file__))
return(False)
except:
return(True)
it also works if the code is run in jupyter notebook.
如果代码在jupyter笔记本中运行,它也可以工作。
#5
-3
If you already have a dependency on matplotlib, or you don't mind introducing one, you can always just call matplotlib.is_interactive()
如果你已经依赖matplotlib,或者你不介意引入一个,你可以随时调用matplotlib.is_interactive()
#6
-6
I make a cover class for testing.
我做了一个测试的封面课。
For example you have :
例如,你有:
class SuperInteractiveClass(object):
def get_data_from_stdin(self):
'... a lot of code here ...'
'... and a lot of other function'
I make a second class, just for testing
我做了第二堂课,只是为了测试
class TestSuperInteractiveClass(SuperInteractiveClass):
prepared_data = []
def add_prepared_data(self,data):
self.prepared_data.append(data)
def get_data_from_stdin(self):
return self.prepared_data.pop(0)
#1
22
This is often works well enough
这通常效果很好
import os, sys
if os.isatty(sys.stdout.fileno()):
...
#2
1
From this link you can use the same way and test if stdin is associated to a terminate(tty), you can do this using os.isatty(), example:
从这个链接你可以使用相同的方式并测试stdin是否与terminate(tty)相关联,你可以使用os.isatty()来做到这一点,例如:
>>> os.isatty(0)
True
N.B: From the same link this will fails when you invoke the command remotely via ssh, the solution given is to test if stdin is associated to a pipe.
N.B:从同一个链接,当你通过ssh远程调用命令时,这将失败,给出的解决方案是测试stdin是否与管道相关联。
#3
-1
if sys.flags.interactive:
#interactive
else:
#not interactive
http://docs.python.org/library/sys.html#sys.flags
http://docs.python.org/library/sys.html#sys.flags
#4
-1
why not symply (python3.X):
为什么不symply(python3.X):
import os
def is_interactive():
'check if current session is interactive'
try:
os.path.isdir(os.path.abspath(__file__))
return(False)
except:
return(True)
it also works if the code is run in jupyter notebook.
如果代码在jupyter笔记本中运行,它也可以工作。
#5
-3
If you already have a dependency on matplotlib, or you don't mind introducing one, you can always just call matplotlib.is_interactive()
如果你已经依赖matplotlib,或者你不介意引入一个,你可以随时调用matplotlib.is_interactive()
#6
-6
I make a cover class for testing.
我做了一个测试的封面课。
For example you have :
例如,你有:
class SuperInteractiveClass(object):
def get_data_from_stdin(self):
'... a lot of code here ...'
'... and a lot of other function'
I make a second class, just for testing
我做了第二堂课,只是为了测试
class TestSuperInteractiveClass(SuperInteractiveClass):
prepared_data = []
def add_prepared_data(self,data):
self.prepared_data.append(data)
def get_data_from_stdin(self):
return self.prepared_data.pop(0)