我正在尝试修复 bug在 flask 脚本中。 Django 曾经有一个 similar bug ,并且 IPython 开发人员建议使用 TerminalIPythonApp 而不是 embed() 以便可以正确加载扩展。但是,django shell 不允许像 flask-script 那样自定义横幅或命名空间。
user_ns 和 banner 通常被传递给 shell 的构造函数,但是 TerminalIPythonApp 类实例化了它自己的 shell 而没有暴露任何可能允许我们传递 shell init 参数的 kwargs。
我想出了以下解决方案,但它有点笨拙。有没有更好的办法?
from IPython.terminal import ipapp
app = ipapp.TerminalIPythonApp.instance()
shell = ipapp.TerminalInteractiveShell.instance(
parent=app,
display_banner=False,
profile_dir=app.profile_dir,
ipython_dir=app.ipython_dir,
user_ns=my_user_ns,
banner1=my_banner)
shell.configurables.append(app)
app.shell = shell
# shell has already been initialized, so we have to monkeypatch
# app.init_shell() to act as no-op
app.init_shell = lambda: None
app.initialize(argv=[])
app.start()
最佳答案
在新的 IPython 1.0 版本中,有一个顶级 IPython.start_ipython()
函数来启动应用程序。这正是您想要的,但不幸的是我们在 1.0 中失误了,将 user_ns
传递给它目前没有任何效果。我们可能会在几周内发布错误修复版本,然后鼓励人们尽可能开始使用新功能。
关于python - 如何在传递自定义 user_ns 和横幅时启动 ipython 应用程序实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18318257/