使用嵌入式 shell 时,如何让 IPython 自动重载魔法自动加载?

Ben*_*vis 6 python ipython

我有以下内容ipython_config.py

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

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

$ ipython
Test autoreload
In [1]: %autoreload 2
In [2]: 
Run Code Online (Sandbox Code Playgroud)

但是,当使用使用 IPython.embed() 的嵌入式 shell 脚本时,自动重新加载魔法不再起作用。

例如,在shell.py

from IPython import embed
embed()
Run Code Online (Sandbox Code Playgroud)

这仍然会加载我的 ipython_config.py,正如"Test autoreload"打印出来的那样,但是没有加载 autoreload 扩展(没有 %autoreload 魔法):

$ python shell.py
Test autoreload
In [1]: %autoreload 2
ERROR: Line magic function `%autoreload` not found.
Run Code Online (Sandbox Code Playgroud)

rem*_*ram 3

据我所知,这是一个(已知)错误。仅当存在应用程序时才会加载扩展,因此当使用嵌入时,它不会被加载(尽管配置被读取)。

github 上有一个开放的问题来解决这个问题,但从未实现过。