Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug sublime plugins during development

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?

like image 291
Azd325 Avatar asked May 05 '13 12:05

Azd325


People also ask

Can you debug in sublime?

Sublime editor includes various plugins that have debugging features, which helps in finding errors easily. It is an extension used for debugging the PHP files and scripts. Provides a list of debugging and profiling capabilities.

Can we debug Python code in Sublime Text?

Use ctrl+shift+b to toggle breakpoint in a line Show activity on this post. 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.


2 Answers

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.

like image 83
Martijn Pieters Avatar answered Oct 02 '22 12:10

Martijn Pieters


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.

like image 31
psclkhoury Avatar answered Oct 02 '22 14:10

psclkhoury