13
votes

I run the following code from convolutional neural network tutorials on jupyter notebook with python 3 kernel, and got the ModuleNotFoundError: No module named 'autoreload';

import numpy as np
import h5py          
import matplotlib.pyplot as plt

%matplotlib inline
plt.rcParams['figure.figsize'] = (5.0, 4.0) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'

%load_ext autoreload   
%autoreload 2          
np.random.seed(1)

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-3d0ea63c7843> in <module>()
      8 plt.rcParams['image.cmap'] = 'gray'
      9 
---> 10 get_ipython().magic('load_ext autoreload   # reload modules before executing user code')
     11 get_ipython().magic('autoreload 2          # Reload all modules (except those excluded by %aimport)')
     12 

/opt/conda/lib/python3.6/site-packages/IPython/core/interactiveshell.py in magic(self, arg_s)
   2156         magic_name, _, magic_arg_s = arg_s.partition(' ')
   2157         magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2158         return self.run_line_magic(magic_name, magic_arg_s)
   2159 
   2160     #-------------------------------------------------------------------------

/opt/conda/lib/python3.6/site-packages/IPython/core/interactiveshell.py in run_line_magic(self, magic_name, line)
   2077                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2078             with self.builtin_trap:
-> 2079                 result = fn(*args,**kwargs)
   2080             return result
   2081 

<decorator-gen-62> in load_ext(self, module_str)

/opt/conda/lib/python3.6/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
    186     # but it's overkill for just that one bit of state.
    187     def magic_deco(arg):
--> 188         call = lambda f, *a, **k: f(*a, **k)
    189 
    190         if callable(arg):

/opt/conda/lib/python3.6/site-packages/IPython/core/magics/extension.py in load_ext(self, module_str)
     35         if not module_str:
     36             raise UsageError('Missing module name.')
---> 37         res = self.shell.extension_manager.load_extension(module_str)
     38 
     39         if res == 'already loaded':

/opt/conda/lib/python3.6/site-packages/IPython/core/extensions.py in load_extension(self, module_str)
     81             if module_str not in sys.modules:
     82                 with prepended_to_syspath(self.ipython_extension_dir):
---> 83                     __import__(module_str)
     84             mod = sys.modules[module_str]
     85             if self._call_load_ipython_extension(mod):

ModuleNotFoundError: No module named 'autoreload'   

I just couldn't find any solution on this issue. How should I fix this error?

2
This is simply an import error (cloaked in magic). I would guess that import autoreload also fails for you. There is something wrong with your python path. The there should by an autoreload.py somewhere in your path, usually in something like .../lib/python3.6/site-packages/IPython/extensions. Look for something similar in sys.path.Marmaduke
Actually, I found the autoreload.py as per your advice. It's in the ...\Lib\site-packages\IPython\extensions. What else might be the cause or what should I do next?Nat
Can you load the module directly with import autoreload? Is the extensions folder in sys.path?Marmaduke

2 Answers

19
votes

The module autoreload belongs to the library IPython, which is running in the background on Jupyter.

The same error occurred to me and it was due to a blank space, see:

snippet code.

So make sure that there are no spaces at the end of the command!

6
votes

I hit this same error because I had a comment after the magic command:

bad:

%load_ext autoreload # this comment should be removed

good:

%load_ext autoreload