lespaul
lespaul

Reputation: 527

importlib.reload() not reloading file into ipython notebook

I have a ipython notebook running in Jupyter Lab. It is running a function that uses variables from an external module I made called "Myfile.py".

Myfile.py includes a few variables that are initially defined like so:

Myterms = ['honda accord', 'ferrari', 'mazerati', 'dodge dumpster']

I have made a few edits to the Myterms list in Myfile.py and then saved the changes to the file.

Myterms = ['Nissan Leaf', 'Tesla Roadster']

What I want to do is reload the Myterms list from Myfile.py into the notebook so that when I call the variables in the list they reflect the changes that have been made.

I've been using a version of code from this example :

import myfile
import importlib

def reloader(myfile):
    importlib.reload(myfile)
    from myfile import Myterms

reloader(myfile)

print(Myterms)

But the line print(myterms) still prints out

['honda accord', 'ferrari', 'mazerati', 'dodge dumpster']

UPDATE: For some reason, the old Myterms list seems to be hanging around in memory and is not being overwritten despite the importlib.reload function. After doing some more research this answer may offer some clues as to why, but I can't find a workaround.

Upvotes: 2

Views: 4689

Answers (1)

Bal Krishna Jha
Bal Krishna Jha

Reputation: 7206

I guess you are looking for: Autoreload available in IPython.

In [1]: %load_ext autoreload

In [2]: %autoreload 2

In [3]: from foo import some_function

In [4]: some_function()
Out[4]: 42

In [5]: # open foo.py in an editor and change some_function to return 43

In [6]: some_function()
Out[6]: 43

source : autoreload

Upvotes: 9

Related Questions