重新加载模块,给出NameError:名称’reload’未定义

问题:重新加载模块,给出NameError:名称’reload’未定义

我正在尝试重新加载已经在Python 3中导入的模块。我知道您只需要导入一次,import再次执行命令将不会执行任何操作。

执行reload(foo)中出现此错误:

Traceback (most recent call last):
    File "(stdin)", line 1, in (module)
    ...
NameError: name 'reload' is not defined

错误是什么意思?

I’m trying to reload a module I have already imported in Python 3. I know that you only need to import once and executing the import command again won’t do anything.

Executing reload(foo) is giving this error:

Traceback (most recent call last):
    File "(stdin)", line 1, in (module)
    ...
NameError: name 'reload' is not defined

What does the error mean?


回答 0

reload 是Python 2中的内置函数,而不是Python 3中的内置函数,因此,您所看到的错误是预期的。

如果您确实必须在Python 3中重新加载模块,则应使用以下任一方法:

reload is a builtin in Python 2, but not in Python 3, so the error you’re seeing is expected.

If you truly must reload a module in Python 3, you should use either:


回答 1

对于> = Python3.4:

import importlib
importlib.reload(module)

对于<= Python3.3:

import imp
imp.reload(module)

对于Python2.x:

使用内置reload()功能。

reload(module)

For >= Python3.4:

import importlib
importlib.reload(module)

For <= Python3.3:

import imp
imp.reload(module)

For Python2.x:

Use the in-built reload() function.

reload(module)

回答 2

import imp
imp.reload(script4)
import imp
imp.reload(script4)

回答 3

要扩展先前编写的答案,如果您想要一个可以在Python版本2和版本3中使用的解决方案,则可以使用以下方法:

try:
    reload  # Python 2.7
except NameError:
    try:
        from importlib import reload  # Python 3.4+
    except ImportError:
        from imp import reload  # Python 3.0 - 3.3

To expand on the previously written answers, if you want a single solution which will work across Python versions 2 and 3, you can use the following:

try:
    reload  # Python 2.7
except NameError:
    try:
        from importlib import reload  # Python 3.4+
    except ImportError:
        from imp import reload  # Python 3.0 - 3.3

回答 4

我建议使用以下代码段,因为它可在所有python版本中使用(需要six):

from six.moves import reload_module
reload_module(module)

I recommend using the following snippet as it works in all python versions (requires six):

from six.moves import reload_module
reload_module(module)

回答 5

为了实现python2和python3的兼容性,可以使用:

# Python 2 and 3
from imp import reload
reload(mymodule)

For python2 and python3 compatibility, you can use:

# Python 2 and 3
from imp import reload
reload(mymodule)

回答 6

如果您不想使用外部库,则一种解决方案是从python 2重新为python 3创建reload方法,如下所示。在模块顶部使用它(假设python 3.4+)。

import sys
if(sys.version_info.major>=3):
    def reload(MODULE):        
        import importlib
        importlib.reload(MODULE)

如果您将python文件用作配置文件,并希望避免重新启动应用程序,则非常需要BTW重新加载。

If you don’t want to use external libs, then one solution is to recreate the reload method from python 2 for python 3 as below. Use this in the top of the module (assumes python 3.4+).

import sys
if(sys.version_info.major>=3):
    def reload(MODULE):        
        import importlib
        importlib.reload(MODULE)

BTW reload is very much required if you use python files as config files and want to avoid restarts of the application…..