Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reloading submodules in IPython

Tags:

python

ipython

Currently I am working on a python project that contains sub modules and uses numpy/scipy. Ipython is used as interactive console. Unfortunately I am not very happy with workflow that I am using right now, I would appreciate some advice.

In IPython, the framework is loaded by a simple import command. However, it is often necessary to change code in one of the submodules of the framework. At this point a model is already loaded and I use IPython to interact with it.

Now, the framework contains many modules that depend on each other, i.e. when the framework is initially loaded the main module is importing and configuring the submodules. The changes to the code are only executed if the module is reloaded using reload(main_mod.sub_mod). This is cumbersome as I need to reload all changed modules individually using the full path. It would be very convenient if reload(main_module) would also reload all sub modules, but without reloading numpy/scipy..

like image 318
Alain Avatar asked Mar 19 '11 18:03

Alain


People also ask

What is the use reload () in Python?

The reload() - reloads a previously imported module or loaded module. This comes handy in a situation where you repeatedly run a test script during an interactive session, it always uses the first version of the modules we are developing, even we have mades changes to the code.

How do you reload a Jupyter module?

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.

What is Autoreload in IPython?

IPython extension to reload modules before executing user code. autoreload reloads modules automatically before entering the execution of code typed at the IPython prompt.


2 Answers

IPython comes with some automatic reloading magic:

%load_ext autoreload %autoreload 2 

It will reload all changed modules every time before executing a new line. The way this works is slightly different than dreload. Some caveats apply, type %autoreload? to see what can go wrong.


If you want to always enable this settings, modify your IPython configuration file ~/.ipython/profile_default/ipython_config.py[1] and appending:

c.InteractiveShellApp.extensions = ['autoreload']      c.InteractiveShellApp.exec_lines = ['%autoreload 2'] 

Credit to @Kos via a comment below.

[1] If you don't have the file ~/.ipython/profile_default/ipython_config.py, you need to call ipython profile create first. Or the file may be located at $IPYTHONDIR.

like image 103
pv. Avatar answered Oct 03 '22 00:10

pv.


In IPython 0.12 (and possibly earlier), you can use this:

%load_ext autoreload %autoreload 2 

This is essentially the same as the answer by pv., except that the extension has been renamed and is now loaded using %load_ext.

like image 20
RafG Avatar answered Oct 03 '22 00:10

RafG