I am working on python code in Visual Studio Code and use several files for functions, which I import at the beginning of a script. Let's say for example I have a file "doStuff.py" in the same folder as my main script with the content
def doStuff():
print('I am doing stuff!')
# print('lots of stuff.')
which I would then import in another script, e.g. "main.py" by writing
from doStuff import doStuff
doStuff()
If I now run the script and afterwards e.g. uncomment the second line of the function in doStuff.py, I would expect to see the new behavior of my doStuff() method. Unfortunately this doesn't happen. I recently switched from Spyder to VSCode and in Spyder this always used to work automatically, but it seems VSCode does not auto-reload the imported modules.
Some info about my current workflow: To open the programming environment, I use "File/Open Folder" and select the folder in which main.py and doStuff.py are located. I am then using "Run Current File in Python Interactive Window" to start my scripts. I am guessing there are better ways and it might have something to do with the launch.json file, but so far the only way I have found to make it use the changed external symbol is restarting VSCode.
Edit: The issue here: Visual Studio Code: Auto-refresh file changes is different, if I understand it correctly, since it is about externally changed files to be reload in VS-Code. My issue is concerned with python modules being reloaded in the interactive window.
Edit 2:
Update: The ultimate solution. Just add this in you code.
%reload_ext autoreload
%autoreload 2
==========================================
I could not find the way make python interactive of vscode auto re-fresh when the .py file changes. but I try importlib.reload()
, it works some way.
import funcs
import importlib
importlib.reload(funcs)
from funcs import *
funcs
is my .py files, and code above should be run when the file changes.
It is now possible to set up autoreload automatically by adding:
"jupyter.runStartupCommands": [
"%load_ext autoreload", "%autoreload 2"
]
to settings.json
.
I would have commented on leo's answer, but I don't have enough reputation. The leo's snippet in VSCode showed me that the setting is unknown. This is what worked for me:
"jupyter.runStartupCommands": [
"%load_ext autoreload", "%autoreload 2"
],
This actually isn't about the Python extension for VS Code but the REPL you're using and what it provides (which I assume is the REPL in the terminal, but you didn't specify if it was that or the interactive window).
Regardless, I honestly find it a bit worrisome that Spyder would do auto-reloading as it has so many "gotcha" situations related to it (as the implementer of import in Python I know first-hand that reloading a module only works in select cases and the one you are suggesting above falls within the category of not getting what you expect due to your from doStuff import doStuff
call in a reliable fashion without some magical hacks that I wouldn't want to rely upon working).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With