3

Possible Duplicate:
How do I unload (reload) a Python module?

Would it be possible to 'refresh' an imported module in python? The use case: interactively improving the module; up til now, I always completely restart the interactive session to reload my updated sources.

Is there a better way?

Community
  • 1
  • 1
xtofl
  • 40,723
  • 12
  • 105
  • 192
  • The accepted answer to [this question](http://stackoverflow.com/questions/437589/) shows how to reload a module (works interactively and in scripts). – Björn Pollex Aug 08 '11 at 19:48

1 Answers1

5

Supposing a module named foo is already present in the namespace, but you have made some changes in source and you want the new code to be imported, use:

reload(foo)

There is a gotcha here: if you have used from foo import bar and you have made subsequent changes in your function bar, then the reload will not work for you. For these cases, you might prefer too use import foo and call foo.bar() so that you can reload for your changes to take effect immediately.

If you are often working in interactive session like this, then perhaps you will be interested to use ipython and add the following lines in your ipy_user_conf.py file:

# For autoreloading of modules (%autoreload, %aimport)    
import ipy_autoreload
wim
  • 338,267
  • 99
  • 616
  • 750