工具

参数:

  • name

    • 解释: 创建的 configuration 的名称
    • 类型: str
  • type

    • 对于 python 项目,设置成 python
  • request

    • 解释: debug 的方式(mode)
    • 类型: str
    • 可选值

      • "attach": debug 一个已经启动的程序(进程)
      • "launch": debug 一个待启动的程序
  • program

    • 解释: .py 文件的路径
    • 可选值:

      • ${file} : debug 过程中当前文件
      • .py 文件的详细路径
      • 相对 project-root 路径

        • eg: "${workspaceFolder}/pokemongo_bot/event_handlers/__init__.py"
  • python

    • 解释:python interpreter 路径
    • 可选值:

      • 不设置, 由环境自行确定
      • python.exe 和 python 命令的路径
  • module

    • 执行的 python 模块名称,类似 python -m my_run_module_to_run
    • 类型: str
    • eg: "my_moduleA.moduleB"
  • pythonArgs

    • 传给 python interpreter 的参数
    • 类型: list[str]
  • args

    • 解释传给 待 debug 的 python 程序的命令行参数
    • 类型: list[str]
  • cwd

    • current word directory, 工作目录
    • 类型: str
    • 可选:

      • 路径
      • "${workspaceFolder}": project-root

vscode 中 debug

要点:配置 .vscode/launch.json 文件

添加 launch.json 方法:

  1. 手动添加
  2. 使用菜单栏添加, 菜单栏 -> Run -> Add Configuration

img/2023-09-12_11-17-07_screenshot.png

添加单个配置

  • 打开 launch.json 文件后,在编辑页面右下角找到 Add Configuration 按钮,点击它

    img/2023-09-12_11-20-51_screenshot.png

debug .py 文件

1
2
3
4
5
6
7
8
{
    "name": "Python: Current File",
    "type": "python",
    "request": "launch",        // 启动类型,可以是launch 或者 attach
    "program": "${file}",       // .py 文件
    "console": "integratedTerminal",
    "justMyCode": true
}

debug 一个 module

1
2
3
4
5
6
7
8
{
    "name": "Python: Module",
    "type": "python",
    "cwd": "${workspaceFolder}", // 启动的 pwd 工作目录,这里设置的是项目根目录
    "request": "launch",
    "module": "ocr_nougat_tool.parser", // module 的路径名称
    "justMyCode": true
}

使用 emacs dap-mode 配置

单个文件

1
2
3
4
5
6
7
(dap-register-debug-template "Python :: Run file from project directory"
                             (list :type "python"
                                   :args ""
                                   :cwd "${workspaceFolder}" ;; 从 project-root 启动
                                   :module nil
                                   :program nil
                                   :request "launch"))

验证可用的 template:

  • project-root .py 文件下

    • Python: This Module
    • Python :: Run file (buffer)

给定 buffer

我自己的动态 template

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
(defun my/dap-python-register-dynamic ()
  (interactive)
  (dap-register-debug-template "Python: This Module"
                               (list :type "python"
                                     :cwd (my/get-project-root)
                                     :module (my/python-get-module-name-cmd)
                                     :env '(("DEBUG" . "1"))
                                     :request "launch"
                                     :justMyCode t
                                     :name (format "Python: This Module" (my/python-get-module-name-cmd)))
                               ))