Reloading module giving NameError: name 'reload' is not defined
Asked Answered
D

7

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?

Debonair answered 7/6, 2009 at 3:55 Comment(0)
K
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:

Kresic answered 7/6, 2009 at 4:41 Comment(6)
This answer makes it sound like it's bad to reload a module in Python 3. What's the thinking behind this?Homochromous
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.Britain
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 @Britain said, though, watch out for import-time side-effects. In general, I would say to avoid reloading someone else's modules. reloading your own makes sense during design-time.Merth
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.Reign
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 hereOrvah
This method might not override other modules' references to the reloaded module. See https://mcmap.net/q/53019/-how-do-i-unload-reload-a-python-module for a solution to that.Importance
S
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)
Squirt answered 29/10, 2015 at 8:19 Comment(1)
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`Celindaceline
M
49
import imp
imp.reload(script4)
Muskogean answered 1/1, 2012 at 0:2 Comment(1)
Deprecated since Python 3.4 - use importlib instead.Foreandafter
S
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
Steinway answered 18/10, 2016 at 22:44 Comment(0)
S
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)
Special answered 21/9, 2017 at 18:47 Comment(1)
This should be the best answer.Camphor
K
5

For python2 and python3 compatibility, you can use:

# Python 2 and 3
from imp import reload
reload(mymodule)
Kacikacie answered 20/8, 2018 at 0:52 Comment(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'Evaporimeter
E
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.....

Erythrocyte answered 7/5, 2019 at 14:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.