张国伟 发表于 2023-12-15 22:59:14

vscode python remote debug极速入门

本文适用范围

主要适用于debug python 程序,尤其是深度学习刚入门需要使用remote 连接到linux进行程序运行,想调试一下的同学。
当然非深度学习也可以参考食用本文哈哈哈。
极速入门版

提前准备:代码仓库已经拉取到linux上面,且已经知道运行的方式。
比如:
项目的启动命令为:python pretrain.py --dataset mini_imagenet --data_path /home/yq/math/miniImageNet --model ResNet12
1.打断点:在你想程序开始debug的地方打上断点
https://img2023.cnblogs.com/other/2630005/202311/2630005-20231112150208771-1979368860.png
2.修改launch.json文件,配置启动的命令:
https://img2023.cnblogs.com/other/2630005/202311/2630005-20231112150209003-775885856.png
https://img2023.cnblogs.com/other/2630005/202311/2630005-20231112150209514-1481870500.png
完成的代码:
{
    // 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": [
// python pretrain.py --dataset mini_imagenet --data_path /home/yq/math/miniImageNet --model ResNet12
      {//python train.py configs/path_to_your_config
            "name": "vil100-pld-orgin", //name 可以自己取
            "type": "python",
            "request": "launch",
            "program": "/home/yq/math/DeepBDC-main/pretrain.py",
            "console":"integratedTerminal",
            "cwd": "/home/yq/math/DeepBDC-main",// current work dir
            "args": [
                "--dataset", "mini_imagenet","--data_path","/home/yq/math/miniImageNet","--model",
                "ResNet12",

            ],
            "justMyCode": true
      },

    ]
}说一下其中关键参数吧:
name:这次debug项目的名字,可以任意取
program:要启动的主程序,个人习惯写绝对路径,好像相对路径也是可以的
cwd:当前工作目录(current work directory),因为代码项目中可能会涉及到使用相对路径,如果不改工作目录的话就可能会出现问题,一般来说这个就改到main.py文件所在的目录,或者按照项目的README.md文件中的目录写即可。
args:附带的参数,每个参数用,隔开。
3.启动:点击绿色三角即可
https://img2023.cnblogs.com/other/2630005/202311/2630005-20231112150209768-1718847345.png
4.程序就会在断点处停下
https://img2023.cnblogs.com/other/2630005/202311/2630005-20231112150209990-2022196961.png
这时候就可以在DEBUG CONSOLE输入需要得到的变量的值,如下:
https://img2023.cnblogs.com/other/2630005/202311/2630005-20231112150210264-1905787555.png
注意:这时候修改了程序的话需要重新启动debug才会按照你修改之后的程序运行,不重新启动的话运行逻辑还是修改之前的
完整版本

完整的debug功能更加强大,甚至可以监听远程窗口,但是目前并没有涉及到使用,具体见官方文档:
Debugging configurations for Python apps in Visual Studio Code
https://dis.qidao123.com/
我的博客园:https://www.cnblogs.com/swx123
我的github(代码一般都放在这里):https://github.com/578223592

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: vscode python remote debug极速入门