Max Henderson
Max Henderson

Reputation: 581

ipython notebook is not updating when I change my code

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?

Upvotes: 48

Views: 48978

Answers (7)

Christian Gomes
Christian Gomes

Reputation: 326

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 autoreload. With autoreload, 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.

Upvotes: 13

zi yuan
zi yuan

Reputation: 91

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.

Upvotes: 3

Tamara Stosic
Tamara Stosic

Reputation: 11

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.

Upvotes: 1

R Kumar
R Kumar

Reputation: 721

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

Upvotes: 31

HLeb
HLeb

Reputation: 661

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

Upvotes: 14

user1629105
user1629105

Reputation: 21

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.

Upvotes: 1

Michael L
Michael L

Reputation: 11

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.

Upvotes: 1

Related Questions