'Reimport a module in python while interactive
I know it can be done, but I never remember how.
How can you reimport a module in python? The scenario is as follows: I import a module interactively and tinker with it, but then I face an error. I fix the error in the .py file and then I want to reimport the fixed module without quitting python. How can I do it ?
Solution 1:[1]
In python 3, reload is no longer a built in function.
If you are using python 3.4+ you should use reload from the importlib library instead:
import importlib
importlib.reload(some_module)
If you are using python 3.2 or 3.3 you should:
import imp
imp.reload(module)
instead. See http://docs.python.org/3.0/library/imp.html#imp.reload
If you are using ipython, definitely consider using the autoreload extension:
%load_ext autoreload
%autoreload 2
Solution 2:[2]
Actually, in Python 3 the module imp is marked as DEPRECATED. Well, at least that's true for 3.4.
Instead the reload function from the importlib module should be used:
https://docs.python.org/3/library/importlib.html#importlib.reload
But be aware that this library had some API-changes with the last two minor versions.
Solution 3:[3]
If you want to import a specific function or class from a module, you can do this:
import importlib
import sys
importlib.reload(sys.modules['my_module'])
from my_module import my_function
Solution 4:[4]
Another small point: If you used the import some_module as sm syntax, then you have to re-load the module with its aliased name (sm in this example):
>>> import some_module as sm
...
>>> import importlib
>>> importlib.reload(some_module) # raises "NameError: name 'some_module' is not defined"
>>> importlib.reload(sm) # works
Solution 5:[5]
Although the provided answers do work for a specific module, they won't reload submodules, as noted in This answer:
If a module imports objects from another module using
from ... import ..., callingreload()for the other module does not redefine the objects imported from it — one way around this is to re-execute the from statement, another is to useimportand qualified names (module.*name*) instead.
However, if using the __all__ variable to define the public API, it is possible to automatically reload all publicly available modules:
# Python >= 3.5
import importlib
import types
def walk_reload(module: types.ModuleType) -> None:
if hasattr(module, "__all__"):
for submodule_name in module.__all__:
walk_reload(getattr(module, submodule_name))
importlib.reload(module)
walk_reload(my_module)
The caveats noted in the previous answer are still valid though. Notably, modifying a submodule that is not part of the public API as described by the __all__ variable won't be affected by a reload using this function. Similarly, removing an element of a submodule won't be reflected by a reload.
Solution 6:[6]
import sys
del sys.modules['module_name']
import module_name
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | CivFan |
| Solution 3 | jss367 |
| Solution 4 | Laryx Decidua |
| Solution 5 | skasch |
| Solution 6 | Boskosnitch |
