如何在Sublime中使用xdebug调试Python脚本

1 投票
1 回答
3434 浏览
提问于 2025-04-18 11:48

我正在尝试从一些比较复杂的开发环境(IDE)转到Sublime这个轻量级的编辑器。我发现Sublime缺少调试功能,而相比之下,像IntelliJ这样的IDE有很强的调试能力。经过一些研究,我了解到XDebug这个工具,它可以提供类似的调试功能。不过,我在设置它的时候遇到了一些问题。

这是我目前做的事情:

  1. 安装了Sublime 3。
  2. 安装了包管理器。
  3. 通过包管理器安装了XDebug。
  4. 发现有些文件缺失,所以我通过包管理器删除了这个包。
  5. 从martomo的GitHub上克隆了这个项目(https://github.com/martomo/SublimeTextXdebug)。
  6. 把它放到了我的Package文件夹里。
    enter image description here

  7. 然后我打开了XDebug.sublime-settings文件,但不太确定该怎么配置(我需要链接到什么,什么URL等等……)

    {
        // For remote debugging to resolve the file locations
        // it is required to configure the path mapping
        // with the server path as key and local path as value.
        //
        // Make sure to use absolute path when defining server path,
        // because Xdebug debugger engine does not return symbolic links.
        //
        // Example:
        // "/absolute/path/to/file/on/server" : "/path/to/file/on/computer",
        // "/var/www/htdocs/example/" : "C:/git/websites/example/"
        "path_mapping": {
    
        },
    
        // Determine which URL to launch in the default web browser
        // when starting/stopping a session.
        "url": "",
    
        // An IDE key is used to identify with debugger engine
        // when Sublime Text will start or stop a debugging session.
        //
        // This package does not filter sessions by IDE key,
        // it will accept any IDE key, also ones that do not match this configured IDE key.
        // It is merely used when launching the default web browser with the configured URL.
        "ide_key": "sublime.xdebug",
    
        // Which port number Sublime Text should listen
        // to connect with debugger engine.
        "port": 9000,
    
        // Show super globals in context view.
        "super_globals": true,
    
        // Maximum amount of array children
        // and object's properties to return.
        "max_children": 32,
    
        // Maximum amount of
        // variable data to initially retrieve.
        "max_data": 1024,
    
        // Maximum amount of nested levels to retrieve
        // of array elements and object properties.
        "max_depth": 1,
    
        // Break at first line on session start, when debugger engine has connected.
        "break_on_start": false,
    
        // Break on exceptions, suspend execution
        // when the exception name matches an entry in this list value.
        "break_on_exception": [
            // E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR
            "Fatal error",
            // E_RECOVERABLE_ERROR (since PHP 5.2.0)
            "Catchable fatal error",
            // E_WARNING, E_CORE_WARNING, E_COMPILE_WARNING, E_USER_WARNING
            "Warning",
            // E_PARSE
            "Parse error",
            // E_NOTICE, E_USER_NOTICE
            "Notice",
            // E_STRICT
            "Strict standards",
            // E_DEPRECATED, E_USER_DEPRECATED (since PHP 5.3.0)
            "Deprecated",
            // 0
            "Xdebug",
            // default
            "Unknown error"
        ],
    
        // Always close debug windows and restore layout on session stop.
        "close_on_stop": false,
    
        // Do not show possible password values in context output.
        "hide_password": false,
    
        // Show in output parsed response instead of raw XML.
        "pretty_output": false,
    
        // Always launch browser on session start/stop.
        // Note: This will only work if you have the 'url' setting configured.
        "launch_browser": false,
    
        // When launching browser on session stop do not execute script.
        // By using parameter XDEBUG_SESSION_STOP_NO_EXEC instead of XDEBUG_SESSION_STOP.
        "browser_no_execute": false,
    
        // Do not use the debugging window layout.
        "disable_layout": false,
    
        // Window layout that is being used when debugging.
        "debug_layout" : {
            "cols": [0.0, 0.5, 1.0],
            "rows": [0.0, 0.7, 1.0],
            "cells": [[0, 0, 2, 1], [0, 1, 1, 2], [1, 1, 2, 2]]
        },
    
        // Group and index positions for debug views.
        "breakpoint_group": 2,
        "breakpoint_index": 1,
        "context_group": 1,
        "context_index": 0,
        "stack_group": 2,
        "stack_index": 0,
        "watch_group": 1,
        "watch_index": 1,
    
        // Custom gutter icons for indicating current line or enabled/disabled breakpoints.
        //
        // Do not use same icon for following values, because Sublime Text is unable
        // to use the same icon for different scopes, in case there are duplicate icons
        // detected it will fall back to the corresponding icon in the package.
        "breakpoint_enabled": "circle",
        "breakpoint_disabled": "dot",
        "breakpoint_current": "",
        "current_line": "bookmark",
    
        // Path to Python installation on your system.
        // Which is being used to load missing modules.
        //
        // It is recommended to configure your Python path for Sublime Text 2
        // especially on older UNIX systems, where some modules (xml.parsers.expat)
        // might be missing and could improve performance of package.
        //
        // Example:
        // "python_path" : "/usr/lib/python2.7"
        "python_path" : "",
    
        // Show detailed log information about communication
        // between debugger engine and Sublime Text.
        // Log can be found at Packages/User/Xdebug.log
        "debug": false
    }
    
  8. 我没有动XDebug.sublime-settings文件,直接运行了调试器。

  9. 访问了http://localhost:9000(结果什么也没发生,页面一直在加载)。

我想调试的脚本在/Users/wf/Desktop(文件名是xdebugTest.py)。

我应该怎么设置URL?(我试过http://localhost:9000/Users/wf/Desktop/xdebugTest.py,结果是404错误)。

如果有人能帮我设置调试参数,我会非常感激。
谢谢你。

1 个回答

3

如果你在Github上克隆源代码的时候,应该会看到一个叫做README的文件,里面其实有关于如何在你的服务器上安装和设置Xdebug系统的重要信息。你可能错过的第一个重要信息是,Xdebug是一个用于PHP服务器的扩展。可以理解,你可能很着急,想赶紧开始,不管怎样。但是,如果你继续阅读这个(名字起得很贴切的)README文件,你会找到指向xdebug.org网站的链接,里面有关于如何在你的服务器上安装这个扩展的详细信息。接着,还有一个很全面的部分叫做配置,它会逐项指导你如何设置.sublime-settings文件,里面还有一些很贴心的注释。最后,还有一个叫做故障排除的部分,教你如何开始一个会话、设置断点和观察表达式、处理异常、定制布局等等。这些文档应该能帮助即使是PHP开发者也能顺利上手。希望下次你遇到类似情况时,能记得去看看这些内容。

撰写回答