Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

revert/undo/redo/refresh, an `import` in interactive python session [duplicate]

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?

like image 877
xtofl Avatar asked Mar 04 '26 17:03

xtofl


1 Answers

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
like image 94
wim Avatar answered Mar 07 '26 07:03

wim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!