Reimport a module in python while interactive
This should work (for Python < 3.4):
From the Python docs
Reload a previously imported module. The argument must be a module object, so it must have been successfully imported before. This is useful if you have edited the module source file using an external editor and want to try out the new version without leaving the Python interpreter.
If running Python 3.4 and up, do
import importlib, then do
Don't forget the caveats of using this method:
When a module is reloaded, its dictionary (containing the module’s global variables) is retained. Redefinitions of names will override the old definitions, so this is generally not a problem, but if the new version of a module does not define a name that was defined by the old version, the old definition is not removed.
If a module imports objects from another module using
from ... import ..., calling
reload()for the other module does not redefine the objects imported from it — one way around this is to re-execute the
fromstatement, another is to use
importand qualified names (
If a module instantiates instances of a class, reloading the module that defines the class does not affect the method definitions of the instances — they continue to use the old class definition. The same is true for derived classes.
In python 3,
reload is no longer a built in function.
If you are using python 3.2 or 3.3 you should:
import imp imp.reload(module)
%load_ext autoreload%autoreload 2
Actually, in Python 3 the module
imp is marked as DEPRECATED. Well, at least that's true for 3.4.
reload function from the
importlib module should be used:
But be aware that this library had some API-changes with the last two minor versions.