launch.json 示例

这个示例包括了几种常用的配置:运行当前文件、调试单元测试、运行模块等。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
"env": {
"MY_ENV_VAR": "value"
}
},
{
"name": "Python: Test with pytest",
"type": "python",
"request": "launch",
"module": "pytest",
"args": ["tests"], // 指定测试目录或单个测试文件
"console": "integratedTerminal",
"justMyCode": false,
"env": {
"TEST_ENV_VAR": "test_value"
},
"envFile": "${workspaceFolder}/.env", // 如果有 .env 文件
"pythonPath": "${workspaceFolder}/venv/bin/python" // 替换为你的虚拟环境路径
},
{
"name": "Python: Run Module",
"type": "python",
"request": "launch",
"module": "mymodule", // 你的模块名称
"console": "integratedTerminal",
"justMyCode": true,
"env": {
"MODULE_ENV_VAR": "module_value"
},
"pythonPath": "${workspaceFolder}/venv/bin/python" // 替换为你的解释器路径
}
]
}

参数说明

通用参数

  • "version":配置文件的版本,一般固定为 0.2.0
  • "configurations":包含一个或多个调试配置。
  • "name":该调试配置的名称,显示在 VSCode 调试面板的配置选项中。
  • "type":调试类型,对于 Python 项目固定为 python
  • "request":请求类型,launch 意味着启动一个新的进程来调试代码。
  • "console":决定调试使用的终端类型:
    • "integratedTerminal":使用 VSCode 内置的终端。
    • "externalTerminal":使用系统默认的外部终端。

具体配置项

  1. Python: Current File

    • "program":要运行或调试的 Python 文件,${file} 指当前打开的文件。
    • "justMyCode":设置为 true 时,将只调试用户代码,忽略第三方库代码。
    • "env":设置环境变量,可以定义调试期间需要的环境变量。
  2. Python: Test with pytest

    • "module": 指定要运行的模块,这里是 pytest
    • "args":为模块传递的参数,这里指定的是测试目录(例如 "tests")。
    • "envFile":指定环境变量文件,一般为 .env
    • "pythonPath":解释器路径,确保使用虚拟环境中的 Python 解释器。
  3. Python: Run Module

    • "module":要运行的 Python 模块(例如 "mymodule")。
    • 其他参数与前面类似。

路径变量示例

  • ${workspaceFolder}:当前打开的工作区根目录。
  • ${file}:当前打开的文件。
  • ${fileDirname}:当前打开文件的目录名。

环境变量管理

  • **环境变量 (env)**:直接在配置中设置,如在 "env" 中添加 MY_ENV_VAR
  • **环境变量文件 (envFile)**:通过 .env 文件管理,适用于大量变量或敏感信息。

怎么使用

  1. 打开 launch.json 文件

    • 在 VSCode 左侧活动栏中,点击 Run and Debug 图标。
    • 点击页面上方的齿轮图标,选择 Python 创建或修改 launch.json 文件。
  2. 选择要运行的配置

    • Run and Debug 面板中,从下拉列表中选择配置名称,比如 Python: Test with pytest
    • 选择后,点击绿色的启动按钮开始调试。

示例 settings.json 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
{
"python.pythonPath": "${workspaceFolder}/venv/bin/python",
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
"python.testing.pytestArgs": [
"tests"
],
"python.testing.autoTestDiscoverOnSaveEnabled": true,
"python.testing.cwd": "${workspaceFolder}",
"python.envFile": "${workspaceFolder}/.env",
"python.testing.pytestPath": "${workspaceFolder}/venv/bin/pytest",
"python.testing.verbose": true
}

参数说明

基本 Python 设置

  1. python.pythonPath:
    • 指定 Python 解释器的路径,例如虚拟环境中的 Python 解释器。
    • 示例:"${workspaceFolder}/venv/bin/python" 表示在项目根目录的 venv 文件夹下的 Python 解释器。

测试框架启用/禁用设置

  1. python.testing.unittestEnabled:

    • 启用或禁用 unittest 测试框架。
    • 示例:false 表示禁用 unittest
  2. python.testing.pytestEnabled:

    • 启用或禁用 pytest 测试框架。
    • 示例:true 表示启用 pytest

pytest 参数设置

  1. python.testing.pytestArgs:

    • 启动 pytest 时传递的参数,通常用于指定要测试的目录或文件。
    • 示例:["tests"] 表示测试项目中的 tests 目录。
  2. python.testing.pytestPath:

    • 指定 pytest 可执行文件的路径。
    • 示例:"${workspaceFolder}/venv/bin/pytest" 表示在虚拟环境中默认的 pytest 路径。
  3. python.testing.verbose:

    • 启用详细输出。
    • 示例:true 表示启用详细输出,这对于调试测试时特别有用。

测试发现和运行设置

  1. python.testing.autoTestDiscoverOnSaveEnabled:

    • 自动测试发现设置,当你保存文件时是否自动发现测试。
    • 示例:true 表示启用自动测试发现。
  2. python.testing.cwd:

    • 指定测试运行时的当前工作目录。
    • 示例:"${workspaceFolder}" 表示以项目根目录为工作目录。

环境变量配置

  1. python.envFile:
    • 指定一个 .env 文件,以便在运行/调试时加载环境变量。
    • 示例:"${workspaceFolder}/.env" 表示使用在项目根目录的 .env 文件。

如何使用这些设置

  1. 创建 .vscode/settings.json 文件

    • 在项目根目录,创建一个 .vscode 文件夹(如果还没有)。
    • .vscode 文件夹中创建一个 settings.json 文件(如果还没有)。
    • 将上述示例内容粘贴到 settings.json 文件中,保存文件。
  2. 定义 .env 文件(可选)

    • 如果项目需要特定的环境变量,可以在项目根目录创建一个 .env 文件。
    • 示例 .env 文件内容:
      1
      2
      MY_ENV_VAR=value
      ANOTHER_ENV_VAR=another_value
  3. 运行测试

    • 打开 VSCode 的 Testing 面板(在左侧活动栏中,带试管图标的)。
    • VSCode 会自动发现并列出所有测试,点击测试项目可以运行测试。
    • 当你保存文件时,如果 autoTestDiscoverOnSaveEnabled 设置为 true,VSCode 会自动重新发现测试。

补充设置(可选)

你可以根据需要添加更多的 VSCode 设置项来优化你的 Python 开发体验:

1
2
3
4
5
6
7
8
{
"python.formatting.provider": "black",
"editor.formatOnSave": true,
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,
"python.linting.flake8Enabled": true,
"python.linting.mypyEnabled": true
}
  • **python.formatting.provider**:设置代码格式化工具,例如 black
  • **editor.formatOnSave**:自动格式化代码,每次保存时自动运行格式化。
  • **python.linting.enabled**:启用代码校验。
  • **python.linting.pylintEnabled**:启用 pylint 校验。
  • **python.linting.flake8Enabled**:启用 flake8 校验。
  • **python.linting.mypyEnabled**:启用 mypy 类型检查。

launch. Json 实例

Golang

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{
// 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": "Launch Vector Agent Server",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/main.go",
"env": {}, // 环境变量
"args": [], // 启动参数配置
"cwd": "${workspaceFolder}"
},
{
"name": "AttachDebug",
"type": "go",
"request": "attach",
"mode": "local",
"processId": "${command:pickGoProcess}",
}
]
}

https://abrance.github.io/2025/04/03/mdstorage/domain/ide/vscode/vscode配置/
Author
xiaoy
Posted on
April 3, 2025
Licensed under