VSCode Python DEBUG 调试

Python VSCode 调试

VSCode Debug

VScode Debug Space

  1. Single file debug -> 选择 Open a file (在没有选中py文件的时候)VSCode Debug Open a file
    py文件必须是单个的可以执行的,不需调用参数文件
    在选中任意一个py文件后,这个选项就会消失
    pyVSCode Debug Selected one py file

  2. 新建Debug的launch file
    CreateVSCodeLaunchFile
    点击后,出现提示
    VSCodeCreatLaunchJsonSelect
    选择 “Python 文件 调试打开的 Python 文件”
    就会生成一个 Launch.json 的文件 在 .vscode 的文件夹下
    VSCodePythonLaunchJson

  • VScode 会在每个项目文件夹下创建一个.vscode文件夹,保存当前项目的运行环境的配置文件。
    VScode debug files

查看选择需要的解释器
selectPyInterpreter
selectInterpreter
编辑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: 当前文件", //需要调试的 py文件要打开并选中
        //     "type": "python",
        //     "request": "launch",
        //     "program": "${file}",
        //     "console": "integratedTerminal",
        //     "justMyCode": true
        // }    
        {
                "name": "Python", // 需要调试的 py文件要打开并选中
                "type": "python",
                "request": "launch",
                "stopOnEntry": false, //是否进入程序就Stop, 报错false is not defined
                "python": "C:\\Users\\YLwork\\APPData\\Local\\Programs\\Python\\Python38\\python.exe", 
                // 自己的Python环境解释器地址, 很重要,如果有多个Python interpreter 的话,需要设置好
                "program": "${file}",
                "cwd": "${workspaceRoot}",
                "console": "integratedTerminal",
                "justMyCode": true, //只调试自己的Code
                "args": ["examples\\benchmarks\\LightGBM\\workflow_config_lightgbm_Alpha158_l1.yaml"], //需要输入的参数
                "env": {},
                "envFile": "${workspaceRoot}/.env",
                "redirectOutput": true
        }
    ]
}
  • 调试的时候,一定要点开选中要调试的py文件

断点 Breakpoint

直接点击文件左侧,激活断点
VScode breakpoint

VSCode Debug Python with VENV 虚拟环境下对python Debug 调试

在 Python 虚拟环境中通过命令行启动调试,您可以使用 debugpy 库。这是一个 Python 调试器,可以让您在 VSCode 或其他编辑器中进行远程调试。

  • 安装 pip install debugpy
  • 在 VSCode 中连接到调试会话: 在 launch.json 中添加一个配置来连接到正在监听的端口。例如
    {
        "name": "Python: Attach",
        "type": "python",
        "request": "attach",
        "connect": {
            "host": "localhost",
            "port": 5678
        }
    }
    
  • 启动调试会话: 使用 debugpy 监听一个端口并等待调试器的连接。例如,监听端口 5678:
    python -m debugpy --listen 5678 --wait-for-client your_script.py
    
    your_script.py 要注意路径
    然后打开要调试的文件,使用F5 开始调试
Reference

VSCode中Python的Debug调试配置
记录VSCode调试python一个极傻的坑:显示launch.json中name ‘true‘ is not defined

Version

0.0.2 - 2025May26

  • 5
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值