34

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

Both SublimeREPL or Python Breakpoints fail with default python build system:

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?

ecoe
  • 4,994
  • 7
  • 54
  • 72
  • 3
    See this: http://stackoverflow.com/a/16385039/4131059 It doesn't fix the `stdin` issue, but it does fix `stdout`. Of course, since for the REPLs, `stdin` is already defined, this skirts the issue nicely. – Alex Huszagh Mar 09 '16 at 19:47
  • 3
    Have you tried the plugin at [github.com/obormot/PythonBreakpoints](https://github.com/obormot/PythonBreakpoints)? – James Vickery May 01 '16 at 13:04
  • Why don't you use eclipse or pycharm which even include remote debugging? Eclipse with pydev will even allow you to inject code while running. Is there a reason that these options cannot be considered? – Rusty Weber Aug 11 '16 at 22:45
  • Have you tried pointing the Python27 executable to the vanilla file? Does the same stack trace come back? – matthew. Aug 24 '16 at 17:06

3 Answers3

5

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 pdb in your code.autocomplete window when the snippet is triggered

Instructions On a 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)

It should start working immediately after saving.

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

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:

<snippet>
    <content><![CDATA[breakpoint()]]></content>
    <tabTrigger>pdb</tabTrigger>
    <scope>source.python</scope>
    <description>Insert a breakpoint</description>
</snippet>
chirinosky
  • 4,438
  • 1
  • 28
  • 39
  • Since python 3.7 you can use built-in breakpoint() function that will do exactly the same - https://www.python.org/dev/peps/pep-0553/ – FamousSnake Nov 16 '19 at 13:13
3

python breakpoint plugin and check this link python breakpoint debugger

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

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

  • visual studio community edition

  • Pycharm

  • Eclipse

  • Komodo

Rajaraman
  • 171
  • 1
  • 3
  • 3
    you've referenced the same sublime tool that I based my question on. – ecoe Aug 20 '16 at 15:52
  • 13
    The existence of IDE's is well known, but much as some people still prefer to use VIM, some people also prefer the light weight and still somewhat powerful sublime text. Having opinions isn't especially relevant to his question. – csga5000 Apr 06 '17 at 15:55
-4

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.

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

Johnny Gasyna
  • 461
  • 1
  • 3
  • 13
  • 1
    trincot - Naturally, I will have to disagree with you. Why use a dull axe when what you actually need is a pair of sharp scissors when cutting some paper, for instance? The solution is to use an editor that makes python development super easy, so that you do not have the breakpoint issue in the first place. Choosing the right tool for the job is the solution here, Furthermore, Pycharm community edition is FREE, so what is wrong with promoting a free product, which is super solid? @Raja seems to agree. – Johnny Gasyna Aug 25 '16 at 21:28
  • 4
    This is not an answer; it's a comment. – Martin CR May 10 '20 at 09:07