Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reload does not work in jupyter notebook, but works in Ipython

I use Anaconda a lot, both jupyter notebook and spyder. Here is my python version: Python 3.7.6 . After I modified some module written by myself, reload always works in spyder's Ipython console, but does not work in jupyter notebook. Here is my reload code:

import imp
imp.reload(my_module)

Here is what jupyter notebook react(It looks like reload works, but changes don't update)

enter image description here

I also tried the following magic command :

%load_ext autoreload
%autoreload 2

Again, these same code work in spyder's Ipython console, but don't work most of the time (yet sometimes work, I can't figure out when the commands work, but I had experienced twice successes) in jupyter notebook. This is the Error reported by jupyter notebook:

    ---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-27-209f44cf5dc0> in <module>
----> 1 get_ipython().run_line_magic('load_ext', 'autoreload ')
      2 get_ipython().run_line_magic('autoreload', '2')

D:\Programs\anaconda3\lib\site-packages\IPython\core\interactiveshell.py in run_line_magic(self, magic_name, line, _stack_depth)
   2315                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2316             with self.builtin_trap:
-> 2317                 result = fn(*args, **kwargs)
   2318             return result
   2319 

<D:\Programs\anaconda3\lib\site-packages\decorator.py:decorator-gen-65> in load_ext(self, module_str)

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

D:\Programs\anaconda3\lib\site-packages\IPython\core\magics\extension.py in load_ext(self, module_str)
     31         if not module_str:
     32             raise UsageError('Missing module name.')
---> 33         res = self.shell.extension_manager.load_extension(module_str)
     34 
     35         if res == 'already loaded':

D:\Programs\anaconda3\lib\site-packages\IPython\core\extensions.py in load_extension(self, module_str)
     78             if module_str not in sys.modules:
     79                 with prepended_to_syspath(self.ipython_extension_dir):
---> 80                     mod = import_module(module_str)
     81                     if mod.__file__.startswith(self.ipython_extension_dir):
     82                         print(("Loading extensions from {dir} is deprecated. "

D:\Programs\anaconda3\lib\importlib\__init__.py in import_module(name, package)
    125                 break
    126             level += 1
--> 127     return _bootstrap._gcd_import(name[level:], package, level)
    128 
    129 

D:\Programs\anaconda3\lib\importlib\_bootstrap.py in _gcd_import(name, package, level)

D:\Programs\anaconda3\lib\importlib\_bootstrap.py in _find_and_load(name, import_)

D:\Programs\anaconda3\lib\importlib\_bootstrap.py in _find_and_load_unlocked(name, import_)

ModuleNotFoundError: No module named 'autoreload '
like image 916
ypdx126 Avatar asked Oct 16 '25 12:10

ypdx126


1 Answers

There is an extra space at the end of the module name which is the cause. I saw a similar stack thread and here it is for your reference.

I ran the magic with the space after the module name and ended up with the same error. Below are the last few lines of the stack trace.

~\anaconda3\lib\importlib\_bootstrap.py in _gcd_import(name, package, level)

~\anaconda3\lib\importlib\_bootstrap.py in _find_and_load(name, import_)

~\anaconda3\lib\importlib\_bootstrap.py in _find_and_load_unlocked(name, import_)

ModuleNotFoundError: No module named 'autoreload '

The solution is to remove the space after the %load_ext autoreload magic. It should be "%load_ext autoreload" and not "%load_ext autoreload ".

The Spyder IPython console seems to do some text processing (trimming spaces) before code execution. Google Colab also does the same and so, the error is not thrown.

like image 67
Kabilan Mohanraj Avatar answered Oct 18 '25 14:10

Kabilan Mohanraj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!