Last updated: Apr 10, 2024
Reading time·2 min
The "NameError: name 'reload' is not defined" occurs when we use the
reload()
function without importing it.
To solve the error, import the reload
function from the built-in importlib
module before using it.
Here is an example of how the error occurs.
import glob # ⛔️ NameError: name 'reload' is not defined new = reload(glob)
To solve the error, import the reload function from the built-in importlib module before using it.
import glob import importlib new = importlib.reload(glob) print(new) # 👉️ <module 'glob' (built-in)>
reload()
function was built-in and directly accessible, however, in Python 3 it has been moved to the importlib
module.The code sample imports the entire importlib
module, however, you can also
import only the reload()
function from the module.
import glob from importlib import reload new = reload(glob) print(new) # 👉️ <module 'glob' (built-in)>
The importlib.reload() method reloads a previously imported module.
The argument to the method must be a module object that has been successfully imported (prior to reloading it).
importlib.reload()
method is most often used when we have edited the source of the module using an external editor and we want to get access to the newer version without exiting the interpreter.The reload()
method returns the module object, so you can directly reassign
the module to the result of calling reload(module)
.