【问题标题】:How to set environment variables with bat file before starting python debugging in vscode在vscode中开始python调试之前如何使用bat文件设置环境变量
【发布时间】:2021-10-08 10:42:45
【问题描述】:

我正在使用 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


    【解决方案1】:

    看看这个Is there any way to set environment variables in Visual Studio Code?。 但据我所知,最简单的方法是:

    1. 打开控制台。
    2. 调用.bat文件
    3. 使用以下代码打开 vscode。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-15
      • 2019-11-26
      • 1970-01-01
      • 2014-04-16
      • 2023-01-24
      • 2020-02-24
      相关资源
      最近更新 更多