Vscode-jupyter: Autoreload other python files if they've been imported

Created on 5 Dec 2018  路  12Comments  路  Source: microsoft/vscode-jupyter

User scenario:

User has two files, foo.py and bar.py.

They are working in foo.py and they run this cell

from bar import my_func
my_func()

Then they go to bar.py and modify my_func().

They run a cell like so:

#%%
my_func()

It would be nice if my_func automatically picked up the changes from bar without having to redo the import.

The autoreload magic does this already.
https://ipython.readthedocs.io/en/stable/config/extensions/autoreload.html

enhancement internal contributor

Most helpful comment

There is an option in the preferences to run startup commands for Interactive Python. Mine is set to
"python.dataScience.runStartupCommands": "%load_ext autoreload\\n%autoreload 2"

All 12 comments

Isn't it possible to simply leverage the existing ipython magic commands? I imagine the following would do the trick:

#%%
%load_ext autoreload
%autoreload 2

This feature is about doing that part automatically, so people wouldn't have to know about autoreload.

I agree that it would be nice if this could be done automatically e.g. via a provided (and user-extensible) startup script that is run whenever a new Python Interactive Window is launched. See https://stackoverflow.com/a/23792621/9629533 for more info about jupyter startup scripts

Actually, I believe this feature request is different from

#%%
%load_ext autoreload
%autoreload 2

As far as I understand, the above only auto-reloads the module as a whole when the source changes, not individual imports from that module. If you import a function as

from bar import my_func
my_func()

then it will not reflect changes. For that you would need to do

import bar
bar.my_func()

Just to be clear, @rchiodo, you're advocating the former, correct? I'd also really appreciate this feature.

@janosh I didn't realize that autoreload didn't work in the 'from' case. Our intent is to track all open files and functions from each file and reload them if they change.

According to the iPython docs, autoreload is actually supposed to support the from case:

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

However, that has never worked for me.

Just ran into a problem with auto-reloading: it doesn't work with decorators, i.e. modifying a decorator does not update previously imported decorated functions.

Took me 15 minutes to figure this out. Here's the corresponding iPython issue. Not that this extension can necessarily do something about it, but I thought I'd mention it anyway in case someone else runs into this issue.

There is an option in the preferences to run startup commands for Interactive Python. Mine is set to
"python.dataScience.runStartupCommands": "%load_ext autoreload\\n%autoreload 2"

鈽濓笍 Syntax has been updated, it's "python.dataScience.runStartupCommands": ["%load_ext autoreload","%autoreload 2"]

@timvink Both still work (see microsoft/vscode-jupyter#913) but the string version now triggers a warning.

Hi @rdrey! We're really sorry for the inconvenience but are looking forward to reviewing your solution to microsoft/vscode-python#3564! We are in the end stages of a restructuring to enable a more manageable relationship with the Python core extension's codebase. We're getting very close to having this completed and once we do we will make a more public statement about it. Please stay tuned.

@greazer No problem. I was basically just going to wrap @krystophny & @timvink's solution in a new setting to append the magic commands to runStartupCommands when it's turned on. I did a search for runStartupCommands in the code base, looked at how microsoft/vscode-jupyter#913 was implemented and was very confused that the code was gone till I read about the new Notebook API.

Was this page helpful?
0 / 5 - 0 ratings