【发布时间】:2016-09-14 21:16:03
【问题描述】:
在我的项目 manage 中,我正在嵌入 iPython:
from IPython import start_ipython
from traitlets.config import Config
c = Config()
c.TerminalInteractiveShell.banner2 = "Welcome to my shell"
c.InteractiveShellApp.extensions = ['autoreload']
c.InteractiveShellApp.exec_lines = ['%autoreload 2']
start_ipython(argv=[], user_ns={}, config=c)
它运行良好并打开了我的 iPython 控制台,但要离开 ipython,我只需键入 exit 或 exit() 或按 ctrl+D。
我想做的是添加一个exit hook 或用其他东西替换那个exit 命令。
假设我有一个函数。
def teardown_my_shell():
# things I want to happen when iPython exits
当我exit 时如何注册要执行的函数,甚至如何让exit 执行该函数?
注意:我尝试通过 user_ns={'exit': teardown_my_shell} 并不起作用。
谢谢。
【问题讨论】: