NameError: name ‘reload’ is not defined in Python [已解决]

NameError: name ‘reload’ 未在 Python 中定义[已解决]

NameError: name ‘reload’ is not defined in Python [Solved]

“NameError: name ‘reload’ is not defined”发生在我们使用函数
reload()而不导入它时。

要解决该错误,请在使用前reload从内置importlib
模块导入函数。

python nameerror name reload is not defined 未定义

下面是错误如何发生的示例。

主程序
import glob # ⛔️ NameError: name 'reload' is not defined new = reload(glob)

错误是如何发生的

使用前先导入reload函数

要解决该错误,请
在使用前

从内置的
importlib模块中导入
reload函数

主程序
import glob import importlib new = importlib.reload(glob) print(new) # 👉️ <module 'glob' (built-in)>

从 importlib 导入重新加载

In Python version 2, the reload() function was built-in and directly accessible, however, in Python 3 it has been moved to the importlib module.

# Importing only the reload function from the importlib module

The code sample imports the entire importlib module, however, you can also
import only the reload() function from the module.

main.py
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).

The 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.

reload()方法返回模块对象,因此您可以直接将模块重新分配给调用的结果reload(module)