Plugin Debugging

Posted on
Fri Apr 14, 2017 6:11 pm
jay (support) offline
Site Admin
User avatar
Posts: 14407
Joined: Mar 19, 2008
Location: Austin, Texas

Plugin Debugging

[MODERATOR NOTE] This is just a repost from our Indigo 7.0 announcement post.

We've added the ability to use a variety of Python debuggers: pdb, PuDB, and PyCharm (professional version). Each has its pros and cons, but all are a significant improvement over self.debugLog()...

In the Plugins Preferences tab, there is a new Development section:

ss88.png
ss88.png (175.85 KiB) Viewed 1338 times


(bet you didn't even remember that tab was there). Because we need to start plugins in a special way for debugging to work, we need to show some special debugging menus. Check the checkbox to see those menus in each plugin's submenu. Next, each debugger needs to be started in a specific way, so select the debugger that you want to use.

By enabling debugging menus, each plugin will have some additional menu items on their submenu:

  • Enable/Reload in Debugger
  • Enable/Reload in Interactive Shell (discussed next)

Selecting the first will enable or restart the plugin with the plumbing enabled for the debugger you selected. We'll talk about the specifics for each next.

pdb

pdb is a command line debugger that's built-in to Python. We'll let you read the docs to find commands and features. What you do need to know is when you select Enable/Reload in Debugger, Indigo will restart your plugin and open a terminal window running pdb:

ss89.png
ss89.png (230.83 KiB) Viewed 1338 times


To add breakpoints to your code, you just add indigo.debugger() method calls where ever you want plugin execution to pause in the debugger. Trying to interactively add breakpoints from pdb or PuDB is hit-or-miss because of the threaded way in which Indigo plugins run. The most reliable to way to force a breakpoint is by manually adding the indigo.debugger() call to the python source and restarting the plugin. Also note indigo.debugger() calls are ignored (NOPs) when the plugin is not launched in debugger mode, so don't lose sleep over leaving an indigo.debugger() call in a shipping plugin.

PuDB

PuDB is a more graphical debugger, much like to the old Borland Turbo debugger from many years ago:

ss90.png
ss90.png (446.59 KiB) Viewed 1338 times


As with pdb, you add breakpoints to your code by adding indigo.debugger() method calls where ever you want plugin execution to pause in the debugger.

PyCharm

Finally, we were able to use PyCharm's remote debugging feature to enable plugin debugging. It requires a bit more setup, but if want to use a fantastic modern IDE, this is the choice for you. Note that because we're using the remote debugging feature, you can only use the professional version of PyCharm as the community edition doesn't support it.

With your plugin's project open in PyCharm, create a run configuration of type Remote Python Debug:

ss91.png
ss91.png (165.5 KiB) Viewed 1338 times


There are three important config parameters in this dialog (you can name the configuration anything you want). The first two tell how to connect: specify localhost in the Local host name box and 5678 in the Port field. The next field you need to adjust is the Path mappings field.

Recall that the recommended way of developing Indigo plugins is to put your plugin in a central location (not inside the Indigo folders), then make a symbolic link to it in the Plugins directory. We do this because the Indigo server moves a plugin between two different folders when enabling/disabling. An IDE/editor will get confused when this happens, so by putting the actual code in a place that never moves and allowing the Indigo Server to move a symbolic link around, you get around this issue.

Because of this, you need to tell PyCharm where the actual path to the source is when the plugin is enabled and being debugged. Click the ellipsis button at the end of the Path mappings field to add a mapping. On the Local path side, you want to specify the actual path to your plugin's source (i.e. /Users/you/path/to/myplugin.indigoPlugin). On the Remote path side, you want to specify the path to your plugin's symlink when the plugin is enabled (i.e. /Library/Application Support/Perceptive Automation/Indigo 7/Plugins/myplugin.indigoPlugin). Tip: to get the path to a file in the Finder, right click on the file to show the contextual menu, then press the Option key. The Copy item will change to Copy as Pathname which is exactly what you want.

One other option is the Suspend after connect checkbox: if you have that checked, then when your plugin restarts with the debugger, it will pause execution in the __init__ method. We don't recommend doing this since you can add breakpoints anywhere you want in the code, including in the __init__ method.

That's it for setup! To debug, just run the remote debug configuration and then restart your plugin in the debugger. Unlike when using pdb and PuDB, you don't need to add explicit breakpoints to the source code using indigo.debugger() – rather, just interactively add breakpoints in PyCharm:

ss92.png
ss92.png (279.33 KiB) Viewed 1338 times


You can step through code, inspect, etc., just like you are debugging any other Python project.

We hope that you'll find a great debugging solution for your needs in one of these options. We've added a couple of new API methods on the plugin objects that are part of this change:

  • plugin.isRunning() – will return true if the plugin is enabled, initialized, and running
  • plugin.restartAndDebug() – a parallel to the restart() method except that it starts the plugin running in the selected debugger

Plugin Specific Interactive Shell

Another great debugging tool is the ability to open a scripting shell that's specific to your plugin's context. This shell is like the more general shell you get when you select the Plugins->Open Scripting Shell menu item, except that because we launch it as part of your plugins startup, it has access to everything in your plugin. You can call methods that your plugin implements, inspect your plugin's objects, etc.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Fri Apr 14, 2017 6:23 pm
jay (support) offline
Site Admin
User avatar
Posts: 14407
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Plugin Debugging

Good news guys - the PyDev open source Python IDE also works with Indigo. While I personally don't like it much (I believe Eclipse, which is the IDE that PyDev is build on, is a hot mess of confusion), it is free and does seem to work for a lot of people. I just downloaded the Mac installer for LiClipse, which is the best way to install a PyDev instance, but it's commercial (after a 30 day trial) so if you want totally free you'll need to install Eclipse and PyDev yourself.

To use it with Indigo, just configure PyCharm as the debugger as described in the post above. Then use the remote debugger feature of PyDev much like you do in PyCharm. I don't have much of any experience with it, so that's about as far as I can take you, but I can say that I only need to follow the first two steps of the remote debugging article (you have to use pip to install pydevd) on the PyDev website, open the plugin, and in Indigo restart the plugin in the debugger. Breakpoints seemed to work as you'd expect, but I didn't thoroughly test it.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Thu Jun 14, 2018 3:52 pm
bsp9493 offline
Posts: 75
Joined: Nov 30, 2017
Location: Kelowna, BC

Re: Plugin Debugging

ok... real newbie question here sorry...

Looked at LiClipse and PyCharm and chose LiClipse... (seems less complex to me and I am totally NEW at this)

After install and reading through the docs, I can get to this point, but that's it.

What am I missing?

Looks like it picks up the plugin when I restart in debugger, but that's it.

Thanx and Sorry for the question in advance

d
Attachments
Screen Shot 2018-06-14 at 2.46.10 PM.png
Screen Shot 2018-06-14 at 2.46.10 PM.png (87.74 KiB) Viewed 531 times

Posted on
Thu Jun 14, 2018 3:59 pm
DaveL17 offline
User avatar
Posts: 4085
Joined: Aug 20, 2013

Re: Plugin Debugging

I don't know that specific tool, but in PyCharm, you need to set up the proper debug profile in PyCharm, include specific code within the Plugin's __init__ method, copy a debugging library to the appropriate Python location AND THEN enable or reload the plugin in the debugger from the Indigo Plugins menu and within PyCharm. You only have to do all these steps when you first set up the debugging profile for that plugin--after that, it's many fewer steps to enable/disable debugging.

It sounds like a lot (and it kind of is) but once you get it set up and working it becomes second nature.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Thu Jun 14, 2018 4:16 pm
bsp9493 offline
Posts: 75
Joined: Nov 30, 2017
Location: Kelowna, BC

Re: Plugin Debugging

Thanx!!

Is there a "setting up a python debugger for dummies tutorial" anywhere?

I'm more of an interactive (trial and error) learner, so was hoping a graphical debugger would make my life easier. For the amount of this that I do PyCharm seems a little excessive for me.

This might be over my head at this time :(

d

Posted on
Thu Jun 14, 2018 4:29 pm
jay (support) offline
Site Admin
User avatar
Posts: 14407
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Plugin Debugging

This post contains instructions for setting up debugging in PyCharm. We have no experience with Liclipse/Eclipse for python/plugin debugging (beyond what I posted above).

FYI, I've always found Intelli-J based IDEs (of which PyCharm is one) much easier to deal with than Eclipse, but that's just me... ;)

[EDIT] a quick search turned up this stackoverflow post which seems to roughly mirror what we do with PyCharm - maybe that will help. You won't need to add the import or call pydevd.settrace because we do that behind the scenes when you have PyCharm selected as your debugger and you restart a plugin in the debugger. We use port 5678.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Thu Jun 14, 2018 5:51 pm
bsp9493 offline
Posts: 75
Joined: Nov 30, 2017
Location: Kelowna, BC

Re: Plugin Debugging

Thanx for the input!!!

I'll give it a shot:)

d

Posted on
Thu Jun 14, 2018 6:20 pm
bsp9493 offline
Posts: 75
Joined: Nov 30, 2017
Location: Kelowna, BC

Re: Plugin Debugging

GOT IT!

I was using the LiClipse calls for the debugger... pydevd.settrace()

Switched them to indigo.debugger() and so far it looks like it works!!!

minimal /no setup with LiClipse. PyCharm looks way more powerful, but also WAY over my head.

YAY...

Thanx for the help.

Posted on
Thu Jun 14, 2018 6:55 pm
jay (support) offline
Site Admin
User avatar
Posts: 14407
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Plugin Debugging

Can you add debug breakpoints in the IDE or do you have to insert indigo.debugger() calls to break?

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Thu Jun 14, 2018 9:08 pm
bsp9493 offline
Posts: 75
Joined: Nov 30, 2017
Location: Kelowna, BC

Re: Plugin Debugging

Not sure at this point.

I appear to be have control to navigate and view, and edit in the debugger...

Cant find a way to add breakpoints at this time. More learning to do.

Currently have it installed and running on my server machine which is running Indigo. Need to do some further reading to determine how to set it up and access via a remote machine.

Yay... more "stuff" to learn :)

Thanx for pointing me in the right direction :)

dave
Attachments
Screen Shot 2018-06-14 at 8.04.17 PM.png
Screen Shot 2018-06-14 at 8.04.17 PM.png (320.88 KiB) Viewed 462 times

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 0 guests