【问题标题】:How to abort python script from IPython (console)如何从 IPython(控制台)中止 python 脚本
【发布时间】:2016-12-12 05:53:52
【问题描述】:

我正在使用来自 IPython(控制台)的 embed() 与我的脚本进行交互。每当我按下 CTRL+D 时,它都会退出交互模式并在embed() 调用之后继续进行下一个推荐。

我如何中止完整的 python 脚本,避免它在交互式 python 中执行embed() 之后的进一步命令?

我尝试过的:

CTRL+C :只取消我在 IPython 中的当前命令

CTRL+D :离开 IPython 并继续执行脚本中的下一个命令

键入 exit() :与 CTRL+D 相同

这是一个示例脚本:

#!/usr/bin/env python

from IPython import embed

print 'hello world'
embed()
print 'I dont want to reach here if I decide to quit from the IPython terminal!'

【问题讨论】:

  • 你说的是 iPython notebook 吗?还是只是 iPython 控制台?
  • 我不知道。我该如何检查?
  • @rkioki 你是如何启动 iPython 的?
  • @user312016 我只是让我的脚本可执行并输入./script.py。我在其标题中使用 #!/usr/bin/env python。
  • 这个关于 stackoverflow 的答案应该会有所帮助:stackoverflow.com/questions/28453165/…

标签: python interactive


【解决方案1】:

一种选择是从 IPython 会话中删除进程 ID:

$ python test_embed.py
hello world
Python 2.7.10 |Anaconda 2.3.0 (x86_64)| (default, May 28 2015, 17:04:42)
Type "copyright", "credits" or "license" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import os

In [2]: os.kill(os.getpid(), 9)
Killed: 9

编辑:看起来使用Ctrl-\ 发送 SIGQUIT 也会完全终止脚本。

【讨论】:

  • 这确实有效,但我想知道是否有更简单的捷径。
  • `Ctrl-` 是我要找的!谢谢完美。
猜你喜欢
  • 2020-02-25
  • 2018-01-05
  • 2018-04-30
  • 2011-10-22
  • 2017-05-10
  • 1970-01-01
  • 2021-05-12
  • 1970-01-01
  • 2020-08-10
相关资源
最近更新 更多