Home »
Python
Reloading modules in Python
Learn about reloading modules in Python programming language, its syntax in various Python versions.
Submitted by IncludeHelp, on March 06, 2022
How to reload modules in Python?
In Python, the reload() reloads a previously imported module. Reloading modules are helpful if you have edited existing modules source files using an external editor and want to use the new version of the modules without leaving the Python interpreter. The return value of reload() is the module object.
When reload() is executed – The code of module is recompiled and the module-level code re-executed, defining a new set of objects which are bound to names in the module's dictionary by reusing the loader which originally loaded the module. The init function of extension modules is not called a second time.
Reloading modules in Python 2.x
In Python 2.x versions, you can simply use reload() method to reload the modules.
Syntax for Python 2.x:
reload(module)
Reloading modules in Python 2.x and <= Python 3.3
In the above versions of 2.x and less than version 3.3, you need to import the imp module and use imp.reload() method by passing the specific module's name to reload the module.
Syntax for above 2.x and <=Python 3.3
import imp
imp.reload(module)
Reloading modules in >=Python 3.4
In the above versions of 3.4, you need to import the importlib module and use importlib.reload() method by passing the specific module's name to reload the module.
Syntax for >=Python 3.4
import importlib
importlib.reload(module)
Reference: importlib.reload(module)