简体   繁体   中英

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

I have the following in my 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
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.

For example, in 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):

$ 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).

There is an open issue on github to fix this, but it has never been implemented.

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

In [1]: %autoreload 2

In [2]: 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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