3

Since IPython Notebook does not reload the file after I modified my module, I am trying to reload it. There was a post on how to do similar things, but my problem is when the .py file is in a subfolder, it does not work.

My original import works like this:

from myutils.MyClassFile import MyClass

while trying to reload:

reload(myutils.MyClassFile)
from myutils.MyClassFile import MyClass

I got the error:

name 'myutils' is not defined

Any suggestion on how to achive this.

Community
  • 1
  • 1
Bin
  • 3,645
  • 10
  • 33
  • 57

1 Answers1

1

In the notebook:

%load_ext autoreload
%autoreload 2

enables auto reload of all imported modules that changed for each execution of a cell.

You can exclude modules with:

%aimport module_to_exclude

Alternatively use:

%autoreload 1

and whitelist what should be reloaded:

%aimport module_to_include
Mike Müller
  • 82,630
  • 20
  • 166
  • 161
  • Does this reload all the modules including the big modules that not been modified? Is there a way to specifically reload one module in a folder instead of all modules in the ? I hope to do so because do not want to reload many large modules (like corpus) every cell run. Thanks. – Bin Dec 20 '15 at 02:19
  • Only modules that changed are actually executed. Added a few more option to my answer. – Mike Müller Dec 20 '15 at 02:28
  • I tried the following: "%autoreload 1; %aimport myutils.myeditedmodule; from myutils.myeditedmodule import MyEditedClass; my_new_obj = MyEditedClass();", which did not reload. Any suggestions? – Bin Jan 02 '16 at 02:37
  • Try: `import myutils.myeditedmodule.MyEditedClass as MyEditedClass`. Also, you need to change and save the file `myeditedmodule` to force a reload. – Mike Müller Jan 02 '16 at 02:44