I love ipython, but I've discovered a problem with %run: imported modules are not reloaded when %run is called repeatedly.
Suppose file ex1.py contains the lines:
import ex2
ex2.x.append(1)
print ex2.x
And file ex2.py contains:
x = []
Now, running python ex1.py from the command line repeatedly prints [1] every time. But invoking %run ex1.py
from within ipython repeatedly prints [1], [1,1], [1,1,1], etc. This is because module ex2.py is not reloaded. So we have a problem: the ipython run-script protocol is not reflecting "real world" behavior.
Note:
%load_ext autoreload
%autoreload 2
does not help. Those lines will get ex2.py reloaded only if a change has been made to the ex2.py file. If we don't make any changes, or only make changes to ex1.py, we get the undesired behavior.
Is there any way to make %run behave like the command line here? This seems like a real deficiency with using ipython as a testing environment for scripts. (Or perhaps the moral is that a module shouldn't be writing into another module's namespace?)
IPython extension to reload modules before executing user code. autoreload reloads modules automatically before entering the execution of code typed at the IPython prompt. The module was reloaded without reloading it explicitly, and the object imported with from foo import ... was also updated.
This happens because when Python imports a module, it runs all the code in that module. After running the module it takes whatever variables were defined in that module, and it puts them on the module object, which in our case is salutations .
1 Answer. You can re-import a module in python, by using the importlib and its function reload.
Simple solution: Use the autoreload to make sure the latest version of the module is used. The autoreloading module is not enabled by default. So you have to load it as an extension. And each time you execute some code, IPython will reimport all the modules to make sure that you are using the latest possible versions.
%run ex1.py
(or any script for that matter) does not do deep reload of your imported module even with the autoreload extension set to 2. It is a "flaw" with how the %run
command works in ipython.
You will have to explicitly call
dreload(ex2)
for a deep reload before executing %run ex1.py
again.
See - http://ipython.org/ipython-doc/dev/api/generated/IPython.lib.deepreload.html
There might be plans to make %run
do deep reload automatically in future and you can find this issue, which is still an open issue at this time of writing, being suggested by a user here - https://github.com/ipython/ipython/issues/461
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With