2

I've seen a few questions asking this, but none of the solutions worked for me.

I am developing a few functions/classes in different modules and have a main.py script that calls everything.

The problem is, when I make a change to a function in another module i.e. module1.py, VSCode does not detect the changes when I call the function in main.py after updating, it's still the older version.

I can get around this by doing something like:

from importlib import reload
reload module1

but this gets old real quick especially when I'm importing specific functions or classes from a module.

Simply re-running the imports at the top of my main.py doesn't actually do anything, I can only do that if I kill the shell and reopen it from the begining, which is not ideal if I am incrementally developing something.

I've read on a few questions that I could include this:

"files.useExperimentalFileWatcher" : true

into my settings.json, but it does not seem to be a known configuration setting in my version, 1.45.1.

This is something Spyder handles by default, and makes it very easy to code incrementally when calling functions and classes from multiple modules in the pkg you are developing.

How can I achieve this in VSCode? To be clear, I don't want to use IPython autoreload magic command.

Much appreciated

FYI here are the other questions I saw, but did not get a working solution out of, amongst others with similar questions/answers :

link1 link2

Derek Eden
  • 4,403
  • 3
  • 18
  • 31

1 Answers1

9

There is no support for this in VS Code as Python's reload mechanism is not reliable enough to use outside of the REPL, and even then you should be careful. It isn't a perfect solution and can lead to stale code lying about which can easily trip you up (and I know this because I wrote importlib.reload() ).

Brett Cannon
  • 14,438
  • 3
  • 45
  • 40
  • well kudos to you ! Thanks for your contribution and for the answer. – Derek Eden May 26 '20 at 20:09
  • I agree its not always reliable enough to use. PyDev in eclipse will live reload when debugging. I wonder if there is a way to get that functional in vs code. – David Mar 18 '21 at 16:39
  • There is support for Flask and Django's auto-reload feature with the debugger. – Brett Cannon Mar 25 '21 at 18:29
  • I'm still confused. Is the solution then for all of us using vscode to reload the window after each module change? – tim.rohrer Jan 27 '23 at 13:08
  • 1
    @tim.rohrer no, there's support built into Django and Flask to detect when files changed and restart their server. VS Code's debugger, debugpy, works with the mechanisms Django and Flask provide to let you continue debug even when those tools reload the code for you. – Brett Cannon Jan 27 '23 at 23:20
  • Thank you for the reply, @BrettCannon. Upon further investigation, I believe the issue I have is related to `pylance`. – tim.rohrer Jan 28 '23 at 13:06