35

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?

Thomas K
  • 39,200
  • 7
  • 84
  • 86
Max Henderson
  • 351
  • 1
  • 3
  • 4
  • can you share a [mcve]? From your error description it's very hard to guess what's going on. – cel Jan 13 '16 at 07:00
  • 14
    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](http://ipython.readthedocs.org/en/stable/config/extensions/autoreload.html) that tries to automatically reload imported modules when they change. – Thomas K Jan 13 '16 at 11:52
  • 1
    Ah! Thank you @ThomasK this ended up working! :) Just an issue of updating the import files. Thanks! – Max Henderson Jan 13 '16 at 15:06
  • I have the same problem! – Soerendip Aug 23 '18 at 22:42

7 Answers7

18

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
R Kumar
  • 471
  • 1
  • 6
  • 6
13

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.

Christian Gomes
  • 316
  • 2
  • 8
12

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

HLeb
  • 591
  • 5
  • 10
2

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.

zi yuan
  • 41
  • 3
0

I have the same issue sometimes. I think it has to do with memory - if I have a bunch of dataframes hanging around it seems to cause issues. If I restart the kernel using the Kernel > Restart option the problem goes away.

Michael L
  • 1
  • 1
0

I have the same problem sometimes. I restarted the kernels but it didn't work.I try to run the cell (ctr+ enter) two or three times. then the result will be displayed according to the updated codes. I hope it helps.

0

insert new empty cell with + option, go to Kernel, choose Restart & Run All. Then, fill in the new inserted cell and run again in Kernel, choose Restart & Run All.

It works with me.

Dharman
  • 30,962
  • 25
  • 85
  • 135