【发布时间】:2021-05-31 16:54:35
【问题描述】:
我想退出 ipython shell,即输入 %exit 然后回车,当按下转义键时。我可以在 shell 中成功键入“%escape”,但很难弄清楚“enter”按。
参考:https://ipython.readthedocs.io/en/latest/config/details.html#keyboard-shortcuts
启动文件夹中的 .py 文件中的代码:
from prompt_toolkit.key_binding import KeyBindings
from IPython import get_ipython
ip = get_ipython()
def exit_ipy(event):
buffer = event.current_buffer
buffer.insert_text('%exit') # just need to figure out how to insert an "enter" press after
buffer.accept_action.validate_and_handle(event.cli, buffer) # I thoght this may work?
ip = get_ipython()
registry = ip.pt_app.key_bindings
registry.add_binding(u'escape')(exit_ipy)
【问题讨论】:
-
愚蠢的评论:您是否尝试在“%exit”之后添加一个“\n”?比如
buffer.insert_text('%exit\n')。 -
我做了,这会产生换行符。也试过
\r。 -
问题是,您希望控制流退出到哪里?退出整个 Python 解释器?在某个时候返回脚本执行?您可以在退出事件处理程序中同时执行这两项操作(例如,只需调用
sys.exit()) -
@JerieWang 我想输入“%exit”,然后输入;我正在调试,我想在第一次退出时退出调试器,例如
-
these suggestions 之一可以工作吗?只是想在这里回答问题背后的问题。 :) 即一个建议是关闭退出确认:
IPYTHON.rc.confirm_exit = False
标签: python ipython key-bindings