如何在vscode中启动python调试前用bat文件设置环境变量

1 人关注

我正在用Adams Car 2020和Python做模拟。我使用Adams内部的Python功能。在这个内部的Python环境中,我没有可用的matplotlib,例如。因此,我创建了一个虚拟的Python环境,其中有额外的软件包,我从Adams Car Python作为一个子进程启动。

在我们启动Adams Car之前,我们用一个批处理文件设置一些环境变量,比方说set_env.bat。当我在虚拟环境中启动python时,我也需要这些环境变量。我可以用一个批处理文件来做,首先调用set_env.bat,然后再调用虚拟环境Python。

start_ext_python.bat。

call set_env.bat
call <path_to_python_virtual_env> %1

其中第一个参数是我想执行的Python脚本的路径。

我想使用Vscode调试并调用set_env.bat。因此我做了一个任务,我在 launch.json 中提到了这个任务。

tasks.json。

// See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ "label": "init_acar", "type": "shell", "command": "C:\\User\\lj012145\\Git\\Adams\\source_a2cm\\bat\\ADAMS_Set_Environment.bat"

launch.json。

// Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ "name": "Python: Test adams external file", "type": "python", "request": "launch", "program": "${file}", "console": "integratedTerminal", "cwd": "${workspaceFolder}", "preLaunchTask": "init_acar", "args": [],

这首先运行了bat文件,但当我在python脚本中停止调试器时,环境变量不再被设置。

我也可以创建一个set_env_vscode.bat,将其写入一个.env文件,然后我再引用,但我不确定这是否是一个好方法。

python
visual-studio-code
vscode-debugger
vscode-tasks
user3223765
user3223765
发布于 2021-08-03
1 个回答
fYre
fYre
发布于 2021-08-04
已采纳
0 人赞同

看一下这个 有什么方法可以在Visual Studio Code中设置环境变量吗? . 但就我所知,最简单的方法是。