I use IPython to interactively use and debug code that I edit in a text editor at the same time. Unfortunately, Python does not automatically reload packages and functions after an initial import foo
, simply for performance reasons.
Fortunately, there is a solution: the IPython extension autoreload does what its name says: either all (how I use it) or only selected (imported using magic function %aimport
) are refreshed, whenever I hit the Enter key. Here’s a short how-i-use-it demo, involving a minimal package foo
with a helloworld function bar
:
[code lang=”python” gutter=”0″]
In [1]: %load_ext autoreload
In [2]: %autoreload 2
In [3]: import foo
In [4]: foo.bar()
Hello World!
In [5]: !cat foo.py
def bar():
print(‘Hello World!\n’)
In [6]: # edit foo.py in editor
In [7]: !cat foo.py
def bar():
print(‘Hello me!\n’)
In [8]: # bar() is automagically reloaded
In [9]: foo.bar()
Hello me!
[/code]
How do you get it? It’s already bundled with IPython by default! Happy hacking!