180

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lonnie Price
  • 10,265
  • 7
  • 24
  • 13

7 Answers7

251

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:

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • 42
    This answer makes it sound like it's bad to reload a module in Python 3. What's the thinking behind this? – ABM Nov 30 '16 at 00:23
  • 13
    Reloading is always problematic. Reloading updates module variables, but does not remove old ones, so if you rename something the old name will still exist. If you change class definitions, existing objects will still have the old type. Finally, some modules run code at import time that isn't designed to run twice. So it is better to avoid reloading, but frequently very convenient. – Evan Jul 10 '17 at 20:03
  • 14
    I would say that it is _often_ problematic, but not _always_. To be sure, I think the valid use cases for writing `reload` into a script are _very_ rare indeed, and those employing that sort of dark art are unlikely to be reading this comment. However, if you are developing a module and using an IPython console to test it interactively, then `reload` can be handy in that work flow. As @Evan said, though, watch out for import-time side-effects. In general, I would say to avoid `reload`ing someone else's modules. `reload`ing your own makes sense during design-time. – Tim D Dec 15 '17 at 18:08
  • 7
    I use Jupyter notebook for my work, and in order to keep things tidy, I put work that should require minimum revisions into importable packages. However, those things sometimes need revisions, and reloading is absolutely the right thing to do, since my notebook kernel is holding in memory calculations that took, literally, all day to compute. – Him May 07 '18 at 16:20
  • Reloading is vital to the active development of libraries with time consuming calls e.g. Database functions. However, one of the main issues you can encounter in reloading modules is that functions with decorators will tend to not reload properly see [here](https://github.com/ipython/ipython/issues/11099) – Alexander McFarlane Jun 25 '19 at 14:00
  • This method might not override other modules' references to the reloaded module. See https://stackoverflow.com/a/61617169/2642356 for a solution to that. – EZLearner May 05 '20 at 15:56
98

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)
Kevin
  • 6,539
  • 5
  • 44
  • 54
  • For me the above two work in case I run for example `imp.reload(plt)` or `imp.reload(plt)` but does not if i run `imp.reload(script)`. plt is set as matplotlib.pyplot by `import matplotlib.pyplot as plt. Do you know why it won't work it i try to reload a script I myself wrote and previously imported? The following error is raced `ModuleNotFoundError: spec not found for the module 'script` – Herwini Nov 26 '20 at 10:16
49
import imp
imp.reload(script4)
41

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
Joey Wilhelm
  • 5,729
  • 1
  • 28
  • 42
13

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

from six.moves import reload_module
reload_module(module)
Alleo
  • 7,891
  • 2
  • 40
  • 30
5

For python2 and python3 compatibility, you can use:

# Python 2 and 3
from imp import reload
reload(mymodule)
Patrick José Pereira
  • 1,613
  • 2
  • 11
  • 12
  • 1
    This works in Python 3.7 (and earlier), but is depricated as @Alex Martelli wrote below. Use Alleo's answer 'from six.moves import reload_module' – Charles Plager Feb 11 '19 at 16:35
4

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

user6830669
  • 161
  • 4