[Note, 20130510: The posting below describes Ipython v. 0.12, which is no longer the newest version. I have more current information in my Notes collection — see the current note or visit the search page for the Notes collection and search for "reload".]
Ipython is a useful application that provides an interactive shell for working with the Python programming language. I find it far better than Idle for all the IDE-like support I need while working with this language.
If you modify your code while running Ipython, there are two ways to make the shell aware of it without restarting the shell:
If you have imported your code as a module, the python command
reload()will reimport the current version of it. For example:
ln : import my_code # my_code.py is now being updated elsewhere ln : reload(my_code)
The updated version of
my_code.pyis now available within the shell, and the shell will confirm that by returning the name of the module, in angle brackets; for instance:
<module 'my_code' from 'my_code.pyc'>
I usually assign a variable name to imported modules so I don't have to type them out at length:
ln : import my_code as M # my_code.py is now being updated elsewhere ln : reload(M) <module 'my_code' from 'my_code.pyc'>
The result is the same as above.
Ipython (v. 0.12) has a module
autoreloadthat reloads not only the module you are running as
main(), but also any other modules called by the main program.
autoreloadmust be explicitly imported, using
import autoreloadin Ipython or the configuration files. After that, entering the magic command
%autoreloadautomatically reloads all relevant modules, including those called by others.
%autoreloadcurrently has a few extensions and commands:
%aimport my_module: import
my_modulealone and mark it for regular reloading with
%aimport -my_module: mark
my_moduleas not to be automatically reloaded
%aimport: without the name of a module, lists those modules marked for regular reloading with
%autoreload 0: disable automatic reloading of modules
%autoreload 1: from now on, always reload those modules marked with
%aimportbefore executing any Python code
%autoreload 2: from now on, always reload all modules (except those marked with
%aimportto be ignored) before executing any Python code
%autoreload: single reload of all modules (except those marked with
%aimportto be ignored)
This accomplishes the same result as
reload()and much more.