[英]How do I drop back to the python shell after exiting a script?
My python script has a couple of sys.exit() calls to terminate under certain conditions. 我的python脚本有几个sys.exit()调用,可以在某些条件下终止。 To test it, I open up a python shell and run
exec(open('script.py').read())
. 为了测试它,我打开了一个python shell并运行
exec(open('script.py').read())
。 Whenever I hit a sys.exit()
call, the script terminates, along with the python shell, which is irritating. 每当我点击
sys.exit()
调用时,脚本就会终止,而python外壳也会终止,这很sys.exit()
。
So I try to get creative and attempt to determine whether my script is run from the commandline or from the shell by examining the stack. 因此,我尝试发挥创意,并尝试通过检查堆栈来确定我的脚本是从命令行运行还是从外壳运行。 Source : Detect if python script is run from an ipython shell, or run from the command line
来源: 检测python脚本是从ipython shell运行还是从命令行运行
def exit_now(exit_status):
os.system('pause')
if PythonShell: # how do I return to the python shell ?
???
else: # exit if we are running from the commandline
sys.exit(exit_status)
...
# check if this was run from inside a python shell
frames = len(inspect.stack())
if frames > 1:
PythonShell = True
else:
PythonShell = False
...
if condition1:
exit_now(1)
if condition2:
exit_now(2)
...
exit_now(0)
How do I drop back to the python shell after terminating a script ? 终止脚本后如何退回到python shell?
python -i <script>
“ i”用于互动。
How about: 怎么样:
try:
exec(open('script.py').read())
except SystemExit:
pass
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.