如何使 VSCode 自动重新加载外部 *.py 模块? [英] How to make VSCode auto-reload external *.py modules?

查看:40
本文介绍了如何使 VSCode 自动重新加载外部 *.py 模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Visual Studio Code 中处理 Python 代码并使用多个函数文件,这些文件是我在脚本开头导入的.例如,假设我在与主脚本相同的文件夹中有一个文件doStuff.py",内容为

def doStuff():print('我在做事!')# print('很多东西.')

然后我会在另一个脚本中导入它,例如main.py"写成

from doStuff import doStuff做东西()

如果我现在运行脚本,然后例如取消注释 doStuff.py 中函数的第二行,我希望看到我的 doStuff() 方法的新行为.不幸的是,这不会发生.我最近从 Spyder 切换到 VSCode,在 Spyder 中这总是自动工作,但似乎 VSCode 不会自动重新加载导入的模块.

关于我当前工作流程的一些信息:要打开编程环境,我使用文件/打开文件夹"并选择 main.py 和 doStuff.py 所在的文件夹.然后我使用在 Python 交互窗口中运行当前文件"来启动我的脚本.我猜有更好的方法,它可能与 launch.json 文件有关,但到目前为止,我发现使用更改的外部符号的唯一方法是重新启动 VSCode.

这里的问题:

解决方案

更新:终极解决方案.只需将其添加到您的代码中即可.

%reload_ext autoreload%自动重载 2

==========================================当 .py 文件更改时,我找不到使 python 交互式 vscode 自动重新刷新的方法.但我尝试了 importlib.reload(),它以某种方式起作用.

导入函数导入导入库importlib.reload(funcs)从函数导入 *

funcs 是我的 .py 文件,当文件改变时,上面的代码应该运行.

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.

这篇关于如何使 VSCode 自动重新加载外部 *.py 模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆