Reputation: 237
When I change an imported python script when running cell by cell imports doesn't work until I restart the whole program VSCode. I tried to kill kernel and restart it, saved changes - nothing works, only restarting is need. But it isn't too quick and appropriate to restart it each time I change the code. Is there any way to apply changes in imported scripts? I am on linux lubuntu 20.04.
Upvotes: 4
Views: 48997
Reputation: 263
A good solution to this problem is using importlib
:
import my_changed_module as mc
import importlib
importlib.reload(mc)
This will reload your module everytime you run your script.
Upvotes: 0
Reputation: 619
This works for me:
%load_ext autoreload
%autoreload 2
Execute those lines after your imports and any saved changes you make to the external file you are importing should get automatically reflected.
Upvotes: 3
Reputation: 1806
When I change an imported python script when running cell by cell imports doesn't work until I restart the whole program VSCode.
If you are trying to change and save an imported python file, running cell by cell does not work like that. You will have to save all the changes in first go, and then try executing cell by cell; that is because in cell by cell execution in VSCode is handled by a stack and no exclusive information like unchanged data/status is available to that stack. That's why you'll have to first make all the changes, and then execute.
Upvotes: 0