繁体   English   中英

在使用嵌入式外壳程序时,如何获取IPython自动重装魔法以自动加载?

[英]How do I get IPython autoreload magic to load automatcially when using an embedded shell?

我的ipython_config.py包含以下ipython_config.py

print "Test autoreload" #confirm this gets loaded
c = get_config()
c.InteractiveShellApp.extensions = ['autoreload']
c.InteractiveShellApp.exec_lines = ['%autoreload 2']

它似乎适用于普通的ipython会话:

$ ipython
Test autoreload
In [1]: %autoreload 2
In [2]: 

但是,当使用使用IPython.embed()的嵌入式Shell脚本时,自动重装魔术不再起作用。

例如,在shell.py

from IPython import embed
embed()

这仍然加载我的ipython_config.py,如打印"Test autoreload"所证明的那样,但是autoreload扩展未加载(没有%autoreload魔术):

$ python shell.py
Test autoreload
In [1]: %autoreload 2
ERROR: Line magic function `%autoreload` not found.

据我所知,这是一个(已知的)错误。 扩展仅在有应用程序的情况下加载,因此在使用embed时不会加载(尽管已读取配置)。

github上有一个开放的问题可以解决此问题,但从未实现。

代替

from IPython import embed
embed()

用这个

from IPython.frontend.terminal.ipapp import TerminalIPythonApp
app = TerminalIPythonApp.instance()
app.initialize(argv=[])
app.start()

您可以运行python shell.py

In [1]: %autoreload 2

In [2]: 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM