Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autoreload of modules in IPython [duplicate]

Is there a way to have IPython automatically reload all changed code? Either before each line is executed in the shell or failing that when it is specifically requested to. I'm doing a lot of exploratory programming using IPython and SciPy and it's quite a pain to have to manually reload each module whenever I change it.

like image 518
Thomas Parslow Avatar asked Dec 15 '09 14:12

Thomas Parslow


People also ask

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.

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.


1 Answers

For IPython version 3.1, 4.x, and 5.x

%load_ext autoreload %autoreload 2 

Then your module will be auto-reloaded by default. This is the doc:

File:       ...my/python/path/lib/python2.7/site-packages/IPython/extensions/autoreload.py  Docstring: ``autoreload`` is an IPython extension that reloads modules automatically before executing the line of code typed.  This makes for example the following workflow possible:  .. sourcecode:: ipython     In [1]: %load_ext autoreload     In [2]: %autoreload 2     In [3]: from foo import some_function     In [4]: some_function()    Out[4]: 42     In [5]: # open foo.py in an editor and change some_function to return 43     In [6]: some_function()    Out[6]: 43  The module was reloaded without reloading it explicitly, and the object imported with ``from foo import ...`` was also updated. 

There is a trick: when you forget all of the above when using ipython, just try:

import autoreload ?autoreload # Then you get all the above 
like image 102
Andrew_1510 Avatar answered Oct 01 '22 18:10

Andrew_1510