ipython notebook is not updating when I change my code

24,219

Solution 1

As Thomas K said, you're probably making a change in an external file that was not imported. There is a very useful command in ipython notebook for such cases, called autoreaload. With autoreaload, whenever you modify an external file you do not have to import it again because the extension takes care of it for you. For more information check: ipython autoreload.

Solution 2

Whenever using external files along with Ipython use autoreload. It will reload the external files every time before executing any code in IPython.

Add this at first cell of the IPython.

%load_ext autoreload
%autoreload 2

Solution 3

For me this was due to one of the following:

  • Cause 1: imported module not updated

Solution:

import importlib
importlib.reload(your_module)
  • Cause 2: other

Solution: restart the kernel, for jupyter notebook this is how enter image description here

Solution 4

I have the same problem. I tried jupyter magic autoreload but it didn't work. Finally, I solved it in this way: in the first cell, add

import My_Functions as my
import importlib
importlib.reload(my)

But notice if the module is imported in this way:

from My_Functions import *

I couldn't reload it properly.

Share:
24,219

Related videos on Youtube

Max Henderson
Author by

Max Henderson

Updated on January 14, 2022

Comments

  • Max Henderson
    Max Henderson over 2 years

    So, I ran into a weird issue using an ipython notebook and not sure what to do. Normally, when I run a part of the code, if there is an error, I would trace it back, fix it, and then re-run the code. I was doing a similar thing but even after making changes to the code, it looks like nothing is changing!

    Here is the example... I am using Python 3.5 so xrange is gone. This then caused an error to be thrown:

    XXXX
         24     XXXX
         25     XXXX
    ---> 26     for t in xrange(0,len(data),1):
         27 
         28         XXXX
    
         NameError: name 'xrange' is not defined
    

    but after changing my code (which you can see below the difference in line 26), the same error pops up!

    XXXX
         24     XXXX
         25     XXXX
    ---> 26     for t in range(0,len(data),1):
         27 
         28     XXX
    
         NameError: name 'xrange' is not defined
    

    Any ideas on why this would be happening?

    • cel
      cel over 8 years
      can you share a minimal reproducible example? From your error description it's very hard to guess what's going on.
    • Thomas K
      Thomas K over 8 years
      I'm guessing you're changing an external file that you've imported. Imported files aren't automatically reloaded. You can explicitly reload it with importlib.reload(mymodule). This especially catches out people used to Matlab, so IPython has an autoreload extension that tries to automatically reload imported modules when they change.
    • Max Henderson
      Max Henderson over 8 years
      Ah! Thank you @ThomasK this ended up working! :) Just an issue of updating the import files. Thanks!
    • Soren
      Soren over 5 years
      I have the same problem!
  • Juan Carlos Ramirez
    Juan Carlos Ramirez almost 5 years
    If it is a memory problem, you could also do %reset, or garbage collection.
  • inkalchemist1994
    inkalchemist1994 almost 4 years
    The %autoreload method wasn't working for me, but using importlib did. Thanks!