8

autoreload doesn't work for me in subdirs at all.

dir structure:

run.ipynb
oof.py
pertussis/
    |-- __init__.py

on run.ipynb I have (running with notebook):

from pertussis import *
check() #defined in the module

this doesn't work. I tried everything. I added the autoreload magic inside code, inside config file, everywhere. I also added the folder of the module to the sys.path list. Never reloaded. I tried reloading a regular file oof.py from the notebook, instead of the module directly.

on oof.py I have:

from pertussis import *
def check_2():
  print ("Hello")

What happend now is that check_2 was autoreloaded successfully, but check from the module still didn't reload.

Nothing seems to work, I am lost.

DeanLa
  • 1,871
  • 3
  • 21
  • 37
  • Am running python 3.5 on Anaconda 4 on win 10 – DeanLa May 29 '16 at 16:18
  • Also tries everything here with no luck: http://stackoverflow.com/questions/33106590/ipython-autoreload-changes-in-subdirectory – DeanLa May 29 '16 at 16:19
  • Sharing **how** you use reload will be usefull, are you using `%aimport` ? Otherwise it won't reload, keep in mind that not everything can be reloaded, and even less deep-reloaded. So you might be just hitting an impossible case. – Matt May 29 '16 at 18:47
  • sorry. I used `%load_ext autoreload autoreload 2` – DeanLa May 30 '16 at 06:11

2 Answers2

4

Sorry for the late response, I've just stumbled upon a similar problem.

In your run.ipynb, have you tried:

import pertussis
pertussis.check()

Or

%load_ext autoreload 
%autoreload 1

then

%aimport pertussis
check = pertussis.check  # optional shortcut
check()
bluu
  • 542
  • 3
  • 13
  • 3
    I tried all of those. The reason is probably because ipython has a hard time doing deep-import. I moved all my sub modules into `.py` files instead of sub-folder with init. – DeanLa Sep 29 '16 at 12:41
  • my class stopped being autoreloaded mysteriously when using `%autoreload` but using `%aimport` fixed it, thanks – pcko1 May 06 '19 at 09:42
3

I don't believe this is an iPython issue, but is due to the nature of from imports.

The following is from Learning Python Oreilly, p798 (5th ed.)

...because from copies (assigns) names when run, there's no link back to the modules where the names came from. Names imported with from simply become references to objects, which happen to have been referenced by the same names in the importee when the from ran.

A workaround is to use import and name qualification. For example:

import pertussis
pertussis.check()

Then in oof.py:

import pertussis # now must use full name qualification
def check_2():
  print ("Hello")
trozzel
  • 479
  • 5
  • 12