VSCode Python调试

直接上手试试

1. 拓展

安装Python (Microsoft)拓展即可.

2. 调试配置

VSCode的调试配置在.vscode/launch.json文件中定义, 可以在右下角添加调试配置. 该Python拓展本身支持多种调试方式, 这里我们最常用的有4种.

  • Python File: 直接调试打开的Python文件, 注意这种方式不带参数, 相当于 python main.py 运行.
  • Python File With Arguments: 直接调试打开的Python文件, 但是可以输入参数.
  • Remote Attach: 通过网络连接调试服务器.
  • Attach using Process ID: 通过进程ID附加, 但是需要对端将ptrace_scope设为0.

注意如果是远程链接的话, 拓展是运行在服务器上的, 所有的地址都是相对服务器而言的.

后两者都是"非侵入式的", 你可以随意断开调试器的连接而不会一并关掉程序, 进程ID附加可以随时想加就加, 随时断开. 但是网络附加只能在wait_for_client的时候附加.

3.1 Remote Attach

Run -> Add Configuration -> Python -> Python Remote Debug Server -> 填写服务器ip / 本地调试localhost 端口5678 配置示例如下:

{
  "version": "0.2.0",
  "configurations": [
        {
            "name": "Python Debugger: Attach using Process Id",
            "type": "debugpy",
            "request": "attach",
            "connect": {
                "host": "localhost", //调试器监听的IP
                "port": 5678 //调试器监听的端口
            },
            "pathMappings": [                                                    
               {                                                                 
                   "localRoot": "${workspaceFolder}",//本地代码所在目录, 这里是当前vscode窗口打开的目录
                   "remoteRoot": "."//远程代码所在目录         
                }                                                                
           ]
        },
    ]
}

3.1.2 代码和Python环境

使用pip安装debugpy包 可以通过两种方法来运行被调试的程序:

  • 直接在源代码中导入debugpy包
import debugpy

debugpy.listen(5678) # 监听端口
debugpy.wait_for_client() # 等待调试客户端连接

debugpy.breakpoint() # 下一行将会被打断点, 也可在vscode中手动打断点
b=2 # 被打断点
a=1 # 不会被打断点
  • 在执行命令中运行调试器
python3 -m debugpy --listen localhost:5678 --wait-for-client main.py <arg1> <arg2>..

3.1.3 连接要调试的程序

  • 确保ip正确.
  • vscode中, 使用快捷键 Ctrl+Shift+D 切换到调试选型卡, 执行刚刚添加的调试配置即可.
  • 调试终端(Debug Console) 可以用于在调试过程中执行一些命令, 语法与Python相同, 但要注意命令的上下文是当前程序断点的上下文, 如输入变量名称可以直接查看变量内容, 将变量赋值将改变变量在执行过程中的值.

4.1 进程ID附加

4.1.2 环境准备

程序运行环境执行如下命令: echo 0 |sudo tee /proc/sys/kernel/yama/ptrace_scope 执行一次即可

vscode中launch.json示例如下:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python Debugger: Attach using Process Id",
      "type": "debugpy",
      "request": "attach",
      "processId": "${command:pickProcess}",
      "console": "integratedTerminal"
    }
	]
}

4.1.3 代码设置

如果你的代码天生慢一拍(运行时间长), 那么可以跳过这个步骤 反之, 可以在你确切想要调试器附加的位置加入如下代码.

import time

c = 0
while c < 1000: # 等待1000秒
    c += 1
    print('.', end='', flush=True)  # breakpoint here, 可注释次语句
    time.sleep(1)

这样你就不会错过你要调试的部分了.

4.1.4 附加

选择 Attach Using Process ID, 选择已运行程序的PID.

5.1 直接运行

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python Debugger: Current File", //不带args
      "type": "debugpy",
      "request": "launch",
      "program": "${file}",
      "console": "integratedTerminal"
    },
    {
      "name": "Python Debugger: Current File with Arguments", //带args
      "type": "debugpy",
      "request": "launch",
      "program": "${file}", //程序路径
      "console": "integratedTerminal", //运行终端配置
      "args": ["${command:pickArgs}"] //参数列表
    }
  ]
}

其中, 参数列表也可提前手动在此处配置好, 格式如下: 对于 --batch_size 5000, 应 ["--batch_size", "5000"]

只需要打开对应的程序, 并且选择如上配置进行调试即可.

  • 注意如果调试器/设备断联则程序也会断掉, 因而建议只做开发用.

  • 这里的配置比较复杂, 自定义程度非常高, 一般的项目其实没必要使用.

Reference


  1. Debugging configurations for Python apps in Visual Studio Code