27

I know this might sound like a really stupid question but whatever. I've made a small script in Python and I've made some changes while in a shell. Normally, on an OS X computer (It's running Python 2.7), I would simply type in reload(the_module) and it would reload my module that includes the changes that I have made. However, when I am reloading the module here (on windows python v. 3.4), it simply gives me this:

>>> reload(instfile)
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    reload(instfile)
NameError: name 'reload' is not defined

And then when I type in imp.reload(my_module), it simply says that the function is deprecated. I can't seem to find what the new function (or it's equivalent) would be anywhere so if someone can help me that would be great! :)

Zizouz212
  • 4,908
  • 5
  • 42
  • 66
  • Better target for dupe is [this](https://stackoverflow.com/questions/961162/reloading-module-giving-nameerror-name-reload-is-not-defined). – John Y Aug 07 '17 at 21:10

1 Answers1

57

The imp module was deprecated in Python 3.4 in favor of the importlib module. From the documentation for the imp module:

Deprecated since version 3.4: The imp package is pending deprecation in favor of importlib.

So, you should be using the reload function from there:

>>> import importlib
>>> importlib.reload
<function reload at 0x01BA4030>
>>> importlib.reload(the_module)
rbrito
  • 2,398
  • 2
  • 21
  • 24
  • 1
    I'm missing a step or what? Still there seems no way to reload a module but to close and reopen the command shell, 80s style. – alejandrob Jan 20 '17 at 22:33
  • 2
    @user3285866 Run the actual function `importlib.reload(MODULE)` –  May 06 '17 at 02:42
  • 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 16:00