让vscode在远程连接服务器时候如本地般顺滑地debug(Python)

https://cloud.tencent.com/developer/article/1840922

 

 

让vscode在远程连接服务器时候如本地般顺滑地debug

【GaintPandaCV导读】本文主要分享了python语言的使用vscode在远程连接服务器的debug,可以通过launch.json来传入python脚本的参数,这样就能够在该情况下用vscode调试,操作跟vscode在本地调试一样

一、vscode 远程连接服务器

1、在vscode应用插件那里下载Remote SSH

Remote SSH

2、连接远程服务器

连接远程服务器

点击SSH TARGETS上面的加号,出现下面的图片,输入ssh username@IP地址,输入密码即可。

SSH TARGETS

3、免密码登录:

在终端输入 ssh-copy-id username@IP地址,输入密码即可。

二、使用vscode在远程服务器上debug

1、命令行的方式:ipdb

首先需要安装 ipdb:pip install ipdb

在终端上输入 python -m ipdb xxx.py就可以一行一行的调试了

或者,在xxx.py文件中在需要中断的地方插入上如下代码

“from ipdb import set_trace

set_trace()”

xxx.py程序跑的时候就会在你设置断点的位置停下来。

但是并不建议使用在源代码中插入代码来达到断点的作用,因为这样破坏了程序源代码的完整性。

纯命令行调试的一些常用指令:

  • h(help):帮助命令
  • s(step into):进入函数内部
  • n(next):执行下一行
  • b(break): b line_number 打断点
  • cl(clear): 清除断点
  • c(continue): 一直执行到断点
  • r(return): 从当前函数返回
  • j(jump): j line_number,跳过代码片段,直接执行指定行号所在的代码
  • l(list): 列出上下文代码
  • a(argument): 列出传入函数所有的参数值
  • p/pp: print 和 pretty print 打印出变量值
  • r(restart): 重启调试器
  • q(quit): 推出调试,清除所有信息

2、直接点击vscode的run进行调试

重点来了,就是使用vscode进行调试,让我们在远程连接服务器的使用感与在本地上一样。没办法,pycharm据说连接远程服务器要收费啊,只能用vscode来做这个事情了。

首先在你项目的文件夹下,创建一个.vscode文件夹,其实也是也可以按按按键来生成的,在ubuntu下,mkdir不是更加便捷嘛hhhh~~。

然后,在.vscode文件夹下面创建3个json文件,launch.json、setting.json、task.json。

a).编写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: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "python": "/home/ml/anaconda3/envs/py36/bin/python", #这个是虚拟环境 conda info --envs 可以看虚拟环境的地址
            "console": "integratedTerminal",
            "args": [
                "--lr",               "0.4",
                "--iter",             "4" ,
                "--epoch",            "30",
                "--model",            "CNN",
              ],
        }
    ]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
sudo vim launch.json
 
{
    "version": "0.2.0",
    "configurations": [
 
        {
            "name": "Python: 当前文件",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "python": "/home/cbpm2016/anaconda3/envs/torch1.7_py36_dc/bin/python",
            "console": "integratedTerminal",
            "args": [
                "--c",               "configs/det/det_mv3_db.yml",
                "--o",             "Global.pretrained_model=./pretrain_models/MobileNetV3_large_x0_5_pretrained"
 
              ]
        }
    ]
}

  

补充一个如何创建虚拟环境和查看虚拟环境

创建虚拟环境: conda create -n name python=3.6
查看虚拟环境: conda info --envs
激活虚拟环境: conda activate
退出虚拟环境: conda deactivate

b).编写setting.json

{
  "python.pythonPath": "/home/ml/anaconda3/envs/py36/bin/python" #这个是虚拟环境 conda info --envs 可以看虚拟环境的地址
}

c).编写task.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "python",
            "type": "shell",
            "command": "/home/ml/anaconda3/envs/py36/bin/python",  #这个是虚拟环境 conda info --envs 可以看虚拟环境的地址
            "args": [
                "${file}"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": [
                "$eslint-compact"
            ]
        }
    ]
}

1
2
3
{
  "python.pythonPath": "/home/cbpm2016/anaconda3/envs/torch1.7_py36_dc/bin/python"
}

  

3、给调试传参数

这个主要是在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": [

    {
           ....
            "args": [
                "--lr",               "0.4",
                "--iter",             "4" ,
                "--epoch",            "30",
                "--model",            "CNN",
              ],
             ....
        }
    ]
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
    "version": "0.2.0",
    "configurations": [
 
        {
            "name": "Python: 当前文件",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "python": "/home/cbpm2016/anaconda3/envs/torch1.7_py36_dc/bin/python",
            "console": "integratedTerminal",
            "args": [
                "--c",               "configs/det/det_mv3_db.yml",
                "--o",             "Global.pretrained_model=./pretrain_models/MobileNetV3_large_x0_5_pretrained"
 
              ]
        }
    ]
}

  

在args里面 传入你自己设定的参数

最后点击 Run and Debug

点击运行和DEBUG

接下来就是选择python解释器,如果没有就直接点击install即可。

这样就完成了,可以愉快地debug了。


本文分享自微信公众号 - GiantPandaCV(BBuf233),作者:LoBob

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2021-06-30

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

posted @   水木清扬  阅读(524)  评论(1编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
历史上的今天:
2018-11-23 Ubuntu16.04上安装cudnn教程和opencv
2018-11-23 Ubuntu 16.04 上安装 CUDA 9.0 详细教程
2018-11-23 Ubuntu16.04安装配置Caffe教程(GPU版)
点击右上角即可分享
微信分享提示