Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Python Debugger (pdb) in Sublime Text 3

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?

like image 386
ecoe Avatar asked Mar 15 '15 17:03

ecoe


People also ask

How do I use pdb debugger in Python?

Starting Python Debugger To start debugging within the program just insert import pdb, pdb. set_trace() commands. Run your script normally and execution will stop where we have introduced a breakpoint. So basically we are hard coding a breakpoint on a line below where we call set_trace().

How do I use debugger in Sublime Text 3?

Installation of Sublime Plugin Step 3 − Start the debug session of PHP file using the shortcut Ctrl+F9. You can also do it using the Start Debug option using command palette.

Does Sublime Text have a Python debugger?

SublimeDebugger. This is a graphical debugger for Sublime Text 3.


2 Answers

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> 
like image 179
chirinosky Avatar answered Sep 20 '22 15:09

chirinosky


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

like image 35
Rajaraman Avatar answered Sep 20 '22 15:09

Rajaraman