I have a function that uses the Python importlib
package to dynamically reload modules which generates some errors when the user is running the code from IPython and has the autoreload
extension loaded and automatic reloading turned on (i.e. %autoreload 2
). What I would like to do is have my code detect whether the user is running IPython, has the extension loaded, and has it set for automatic reloading. If so, disable it, run my block of code, then re-enable it.
I believe I can use the following code to detect whether we are in IPython and run magic functions, but I cannot figure out how to programmatically check whether autoreload
has been loaded and whether %autoreload 2
is set.
from IPython import get_ipython
ipy = get_ipython()
if not ipy is None:
# Need to check for %autoreload 2 here
ipy.magic("%autoreload 0")
# Run some code
ipy.magic("%autoreload 2")