简体   繁体   English

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

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

I have the following in my ipython_config.py : 我的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']

And it seems to work for normal ipython sessions: 它似乎适用于普通的ipython会话:

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

However, when using an embedded shell script that uses IPython.embed(), the autoreload magic no longer works. 但是,当使用使用IPython.embed()的嵌入式Shell脚本时,自动重装魔术不再起作用。

For example, in shell.py : 例如,在shell.py

from IPython import embed
embed()

This still loads my ipython_config.py, as evidenced by "Test autoreload" printing out, however the autoreload extension does not get loaded (no %autoreload magic): 这仍然加载我的ipython_config.py,如打印"Test autoreload"所证明的那样,但是autoreload扩展未加载(没有%autoreload魔术):

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

As far as I can tell, this is a (known) bug. 据我所知,这是一个(已知的)错误。 Extensions are only loaded if there is an Application, so when using embed, it won't get loaded (although the config is read). 扩展仅在有应用程序的情况下加载,因此在使用embed时不会加载(尽管已读取配置)。

There is an open issue on github to fix this, but it has never been implemented. github上有一个开放的问题可以解决此问题,但从未实现。

Instead of 代替

from IPython import embed
embed()

Use this 用这个

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

You can run python shell.py 您可以运行python shell.py

In [1]: %autoreload 2

In [2]: 

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

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