在 Sublime Text 3 中运行 Python 调试器 (pdb)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29064081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:02:47  来源:igfitidea点击:

Run Python Debugger (pdb) in Sublime Text 3

pythonsublimetext3breakpointssublime-text-pluginsublimerepl

提问by ecoe

How can you set python debugger (pdb) breakpoints in Sublime Text 3?

如何在 Sublime Text 3 中设置 python 调试器(pdb)断点?

Both SublimeREPLor Python Breakpointsfail with default python build system:

无论SublimeREPLPython的断点失败,默认的Python构建系统:

print "hello"
# code runs fine without this breakpoint
import pdb; pdb.set_trace()
print "bye"

>>>File "C:\Python27\lib\bdb.py", line 49, in trace_dispatch
>>>return self.dispatch_line(frame)
    >>>File "C:\Python27\lib\bdb.py", line 68, in dispatch_line
    >>>if self.quitting: raise BdbQuit

Above issue appears documented, but with no obvious fix. Or has the answer been staring at me?

上述问题似乎已记录在案,但没有明显的修复。还是答案一直盯着我看?

回答by Johnny Gasyna

You could try to use an IDE specific for Python which makes debugging and setting up python projects really easy. I would recommend you try the free community version of Pycharm.

您可以尝试使用特定于 Python 的 IDE,这使得调试和设置 Python 项目非常容易。我建议您尝试 Pycharm 的免费社区版本。

https://www.jetbrains.com/pycharm/download/

https://www.jetbrains.com/pycharm/download/

回答by Rajaraman

python breakpoint plugin and check this link python breakpoint debugger

python 断点插件并检查此链接python 断点调试器

Use ctrl+shift+b to toggle breakpoint in a line

使用 ctrl+shift+b 在一行中切换断点

But its not preferable solution for debugging a software using a text editor. There are best IDE which makes your development lot easier

但它不是使用文本编辑器调试软件的首选解决方案。有最好的 IDE 让你的开发更容易

  • visual studio community edition

  • Pycharm

  • Eclipse

  • Komodo

  • 视觉工作室社区版

  • 皮查姆

  • 科莫多

回答by chirinosky

If you don't want to deal with additional packages, you can create a snippet to set the breakpoint for you.

如果你不想处理额外的包,你可以创建一个片段来为你设置断点。

<snippet>
    <content><![CDATA[import pdb;pdb.set_trace()]]></content>
    <tabTrigger>pdb</tabTrigger>
    <scope>source.python</scope>
    <description>Insert a breakpoint</description>
</snippet>

The above snippet will trigger whenever you type pdbin your code.autocomplete window when the snippet is triggered

每当您输入pdb代码时,上面的代码段就会触发。触发片段时的自动完成窗口

Instructions On a Mac

Mac 上的说明

  • Navigate to Tools -> Developer -> New Snippet
  • Replace the template with the snippet above
  • Save the snippet to ~/Library/Application Support/Sublime Text 3/Packages/User
  • Ensure the name ends with sublime-snippet (ex. breakpoint.sublime-snippet)
  • 导航到工具 -> 开发人员 -> 新代码段
  • 用上面的代码片段替换模板
  • 将代码片段保存到 ~/Library/Application Support/Sublime Text 3/Packages/User
  • 确保名称以 sublime-snippet 结尾(例如 breakpoint.sublime-snippet)

It should start working immediately after saving.

它应该在保存后立即开始工作。

---------- UPDATE 29 NOV 2019 ----------

---------- 2019 年 11 月 29 日更新 ----------

As mentioned by @FamousSnake in the comments below, you can modify the snippet to use the built-in breakpoint()function if you're using Python 3.7 or higher. This is especially convenient if you use linters or utilities like black to format your code. It should get them to stop complaining or splitting the code above into multiple lines. You can use the revised snippet below:

正如@FamousSnake 在下面的评论中提到的,如果您使用的是 Python 3.7 或更高版本,您可以修改代码片段以使用内置的breakpoint()函数。如果您使用 linter 或 black 等实用程序来格式化代码,这将特别方便。它应该让他们停止抱怨或将上面的代码分成多行。您可以使用以下修改后的代码段:

<snippet>
    <content><![CDATA[breakpoint()]]></content>
    <tabTrigger>pdb</tabTrigger>
    <scope>source.python</scope>
    <description>Insert a breakpoint</description>
</snippet>