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?

1
  • The accepted answer to this question shows how to reload a module (works interactively and in scripts). Commented Aug 8, 2011 at 19:48

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.