2

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.

4
  • With the code you've posted, searchterms would be a module, not a list. Commented Jun 11, 2018 at 4:02
  • I just changed the module name to make things a little clearer. Commented Jun 11, 2018 at 4:15
  • Haven't you tried myfile = importlib.reload(myfile)? Commented Jun 11, 2018 at 4:18
  • I just did. It didn't work. Commented Jun 11, 2018 at 4:25

1 Answer 1

9

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.