如何在 VS Code 中调试带参数和环境变量的 Python 程序

安装 Python 插件

点击左侧最下方的 "扩展"图标(四个方块组成的图标 🧩),或者按快捷键 Ctrl+Shift+X (Windows/Linux)或 Cmd+Shift+X(Mac)。在搜索框输入 Python。找到由 Microsoft 发布的 Python 插件(图标是蛇🐍),点击 Install。

安装完成后,左边会出现一些新图标(比如资源管理器、搜索、源代码管理等)。如果左边还是空白,点顶部菜单:View → Appearance → Show Activity Bar(确保侧边栏显示)。

打开项目文件夹

在 VS Code 中,点击顶部菜单:File → Open Folder(或 Ctrl+K Ctrl+O )。

选择你的项目根目录,打开后,左边就会显示文件树(资源管理器)✅

选择正确的 Python 解释器

因为你用了 conda 虚拟环境 envName,VS Code 需要知道用哪个 Python。

按 Ctrl+Shift+P (命令面板),输入 Python: Select Interpreter,回车。

在弹出的列表中,找到你的 conda 环境路径,例如:

~/miniconda3/envs/envName/bin/python

或

/opt/conda/envs/envName/bin/python

选中后,左下角状态栏会显示当前 Python 环境名称(如 envName)。

设置断点(Breakpoint)

在左侧文件树中,点击打开 train.py。

找到你想调试的代码行(比如 main() 函数开头,或报错附近的行)。

点击行号左侧的空白处,会出现一个红点🔴 ------ 这就是断点!

创建调试配置(launch.json)

点击左侧 "运行和调试"图标(虫子🐛图标)。如果没看到,按 Ctrl+Shift+D 。

点击 "create a launch.json file" → 选择 Python File。

VS Code 会自动生成 .vscode/launch.json 文件,内容类似:

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 Debugger: Current File",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        }
    ]
}

如果需要添加参数和环境变量,修改 launch.json,加入你的参数

将内容替换为以下配置(注意 args 字段):

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 Debugger: Current File",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "args": [
                "benchmark=xxx",
                "run_name=xxx",
            ],
            "console": "integratedTerminal",
            // "envFile": "${workspaceFolder}/.env",
            "env": {
                "xxx_API_KEY": "xxx"
            },
            "justMyCode": true
        }
    ]
}

开始调试

确保你已经在 train.py 中设置了断点(红点🔴)。

点击左侧 运行和调试 面板顶部的绿色三角形 ▶️(或按 F5)。

VS Code 会:

启动你的虚拟环境;运行 train.py 并传入参数;在断点处暂停!

暂停后,你可以:

查看变量值(鼠标悬停或在"变量"面板);

按 F10 单步跳过,F11 单步进入;

在"终端"中看到输出。

相关推荐
默_笙2 天前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
小羊没烦恼!2 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
qq_426003962 天前
启动playwright录制codegen生成自动化测试脚本
python·自动化
虎头金猫2 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
长沙三为智能科技2 天前
家政小程序开发从0到上线:五阶段交付流程与验收清单
python
伞伞悦读2 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
只睡四小时2 天前
Canvas 弹道联机实战:700 行 + 固定时间步长
python·websocket·html5·游戏开发·canvas
C语言小火车2 天前
C/C++ 为什么需要编译器?
开发语言·c++
奇思妙想聪明勤奋的小羊3 天前
DeepAgents第5章:子Agent 与上下文隔离—让 Agent学会委派
人工智能·python·学习·语言模型
lpfasd1233 天前
2026年第38周GitHub趋势周报
python·科技·github