Reloading module giving 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?


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:

  • importlib.reload for Python 3.4 and above
  • imp.reload for Python 3.0 to 3.3 (deprecated since Python 3.4 in favour of importlib)

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)

import imp
imp.reload(script4)