For example, in my folder, I have my ipython notebook "foo.ipynb" and a python file "bar.py" which has some functions in it (for example, "func") that is going to be used in "foo.ipynb".
$ ls
foo.ipynb bar.py
However, there is a bug in "func" in "bar.py"
When I first called the "func" in "foo.ipynb", it reported a bug.
Then I opened "bar.py" in my editor(by "$ vim bar.py") , fixed that bug, and save the file.
However, when I call "func" again inside the notebook, it reported the same bug again which I am sure I have fixed it.
Then, I copy my "bar.py" to "bar2.py", and "from bar2 import func", this time it worked!
Then I try "from bar import func" again and it reported bug again even the code of func in "bar.py" and "bar2.py" are the same.
I don't know whether my description of problem makes sense and if you have other questions about this issue, feel free to ask me.
This is the way Python handles imports - when you from bar import func, it reads the file and gives you func. Once it's done that, changing the code in the file has no effect - Python isn't looking at it again unless you ask it to reload the file. You can
@takluyver Hi, I am having this issue but only the first of the above works to reload it. This is problematic as I am loading large-ish amounts of data before hand.
Why might this be? Is there anything I can provide that might help debug this? Thanks.
I don't think reloading a module itself can silently fail, but if you've e.g. instantiated a class defined in the module before you reload it, the instance will still have the old class.
You may want to work with code in the notebook, where it's easy to modify and re-run, until it's solid enough to move out to a separate file.
Sorry, just to clarify, you're saying if I import a submodule or a class from a package e.g.
from my_module import class_or_submodule as clsm
import importlib
importlib.reload(clsm)
Then this would not work with a reload? However this would,
import my_module
import importlib
importlib.reload(my_module)
Your second point is also useful. In this case I was moving functions from Jupyter to class methods. Still, I can reimport the whole module as above.
import foo
import importlib
a = foo.SomeClass()
# Made some changes to foo.py
importlib.reload(foo)
b = foo.SomeClass()
# This still calls the old method
a.method()
# This calls the new one
b.method()
type(a) is not type(b) # Because type(b) is the redefined SomeClass
Oh, I was a fair bit off! Many thanks for the concrete example.
this issue is fixed restarting the kernel in your notebook every time you change or edit your py file
I got the same error. This is how I sorted that.
Use importlib
If python file is in the same path:
import importlib
importlib.reload(<python file name to be imported>)
If python file is in some other folder
import importlib
from utils import <file name>
importlib.reload(<file name>)
Most helpful comment
This is the way Python handles imports - when you
from bar import func, it reads the file and gives youfunc. Once it's done that, changing the code in the file has no effect - Python isn't looking at it again unless you ask it to reload the file. You can