How to debug sublime plugins during development
Solution 1
The problem is that sys.stdin
is not attached to anything normally. But, sys.stdin
does work if you start SublimeText2 from a console:
-
On Mac, start the application by locating the executable in the resource bundle by entering the full path in the Terminal:
/Applications/Sublime\ Text\ 2.app/Contents/MacOS/Sublime\ Text\ 2
-
On Windows, start the application from the Windows Console:
"C:\Program Files\Sublime Text 2\sublime_text.exe"
provisional, I have no Windows Sublime Text 2 install so this command line is based on a quick Google
Now the application has a console; but sys.stdout
is still redirected to the built-in SublimeText 2 console. You want to start your debugger with the correct stdout
, the one still connected to your console. Instead of import pdb; pdb.set_trace()
, use:
import pdb, sys; pdb.Pdb(stdout=sys.__stdout__).set_trace()
The original console stdout
is saved in sys.__stdout__
and by passing that to pdb.Pdb()
you get a fully functional pdb
session.
Solution 2
The easiest way I found was to use Visual Studio.
You should have the Python development tools for Visual Studio (you can download them by opening the Visual Studio Installer, clicking on more, modify and selecting Python development).
To debug you need to open visual studio, select from the toolbar: Debug - Attach to process (Ctrl+Alt+P) and then find 'plugin_host.exe' and attach. Then open your file 'plugin.py' and put a breakpoint somewhere and you're good to go.
Solution 3
Working on my plugin, I didn't have much luck with pdb, and "print" is not an efficient debugging experience (for example, if you aren't sure where the defect is, you might add a lot of "print" - and then have to remove them after).
There is a much better alternative if you run Windows. The latest version 2.2 of the Python tools for Visual Studio works great for debugging Sublime plugins. You get all the regular debugging features of Visual Studio and it's a polished experience. Just choose "pluginhost.exe" and the Python debugging engine in the attach dialog. Previous to 2.2, the Python tools did not work properly against Sublime, for example stepping was broken.
Disclosure: I work in Visual Studio but do not work on these tools. I recently worked with the Python tools developer to fix the bugs I encountered using these tools to write my plugin.
The Community Edition of Visual Studio 2015 is free to individual developers and small organizations. Just make sure you check Python tools in the setup dialog. And, of course, you must be running Windows.
Solution 4
Your problem is that sys.stdin
and (Edit: stdout goes to the console) are connected into the internals of sublime text - where do you expect to be able to control the debugger?sys.stdout
What you want is a remote debugging interface that interacts through something other than stdio, such as rpdb
.
Related videos on Youtube
Azd325
You can find my blog here about Django and Git and some more techie things BLOG
Updated on June 11, 2022Comments
-
Azd325 almost 2 years
I want to debug my plugin with pdb but it doesn't work. I get these errors
Traceback (most recent call last): File "./sublime_plugin.py", line 362, in run_ File "./useIt.py", line 14, in run for region in self.view.sel(): File "./useIt.py", line 14, in run for region in self.view.sel(): File ".\bdb.py", line 46, in trace_dispatch File ".\bdb.py", line 65, in dispatch_line bdb.BdbQuit
Has anyone an idea? Or some other way to debug a sublime plugin?
-
Martijn Pieters about 11 years@Eric: The stack trace ends in a Python std library, it is clear enough to me.
self.quitting
has been set to aTrue
value and the debugger exits by using an explicit exception. Now, whyself.quitting
is set to aTrue
value isn't known, unfortunately that would require debugging the debugger and Sublime internals.
-
-
Azd325 about 11 yearsThanks for it. I will have a take a look.
-
Eric about 11 yearsThis doesn't seem to work for me in windows - the windows console returns immediately to the prompt, launching sublime text in the background
-
Martijn Pieters about 11 years@Eric: Right, I feared that might be the case. There is a 'remote' command line and then there is the application itself. Is there a second .exe in the
Sublime Text 2
folder? Or does thesublime_text.exe
command line take command line switches? DoesSublimeText.exe
exist in the same location? The FAQ suggests there is. -
Eric about 11 yearsOn your system, does this also make
raw_input()
work in the sublime text console, deferring to the console input? -
Eric about 11 yearsNope, no second exe, and I'm struggling to find the command line options.
-
Martijn Pieters about 11 years@Eric: This makes
raw_input()
work, albeit that the prompt is not output on the terminal; but input from the terminal is read correctly. -
Azd325 about 11 yearsSo I tested it on my Linux. I work nearly great but is it not possible to access already set variables but I can call function and like this.
-
Martijn Pieters about 11 yearsIndeed, it could be that variables you have set in the SublimeText2 console won't be visible to the debugger; different namespaces. But otherwise you have full access to the python interpreter and it's state.
-
Azd325 about 11 yearsI don't set the variables in console I set it in my code but yeah is only nice to have feature
-
schlamar almost 11 yearsIn theory, there is the
-w
or--wait
flag which should prevent that the console is loosing focus. And this works fine with some blocking editor aware applications (likehg commit
). But oncmd
it still returns without waiting. -
AlexanderLedovsky over 10 yearsThank you! Its really awesome!
-
flaviussn almost 8 yearsYou can also debug with PyCharm Community. Is free and multi-platform python IDLE
-
Just a learner almost 6 yearsHi @cheerless, I installed Visual Studio and attached "plugin_host.exe". I don't what to do next to start the debugging. Could you please describe a little about the operations? Thanks you.