How to use IPython magic within a script to auto reload modules?

拟墨画扇 提交于 2019-12-08 14:15:46

问题


I am trying to include some of the IPython built-in magic functions to auto reload modules when I am running a script. So I have tried this:

if __IPYTHON__:
    %load_ext autoreload
    %autoreload 2

But IPython returns:

%load_ext autoreload
^
SyntaxError: invalid syntax

Any idea how to solve this?


回答1:


Thank you for the link Gall!!! Whit your help I came up with the following solution:

from IPython import get_ipython
ipython = get_ipython()

if '__IPYTHON__' in globals():
    ipython.magic('load_ext autoreload')
    ipython.magic('autoreload 2')


来源:https://stackoverflow.com/questions/32906669/how-to-use-ipython-magic-within-a-script-to-auto-reload-modules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!