# How To: Debug Plex Add-in code

**URL:** <https://forums.plex.tv/t/how-to-debug-plex-add-in-code/164343>\
**Category:** Dev/API Corner\
**Tags:** plugin-dev\
**Created:** [October 23, 2016, 9:14pm UTC](https://forums.plex.tv/t/how-to-debug-plex-add-in-code/164343 "2016-10-23T21:14:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![baradanikto](https://avatars.discourse-cdn.com/v4/letter/b/5e9695/32.png) [@baradanikto](https://forums.plex.tv/u/baradanikto)\
**Post date:** [October 23, 2016, 9:14pm UTC](https://forums.plex.tv/t/how-to-debug-plex-add-in-code/164343/1 "2016-10-23T21:14:47Z")

</div>

I’m running the Plex server on my Synology NAS drive. I’ve found a bug in some channel code that I loaded. I’d like to fix it, but, I don’t know ho to go about debugging it. I’d like to walk through the code to make sure my suspicions are correct and that my fix actually works. Is there any way I can do that?

I have experience writing channels for Roku. I can connect a box, side load my code, and step through it to debug. Does the equivalent process exist for Plex?

---

<div class="post-metadata">

**Author:** ![dane22](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/dane22/32/4389_2.png) [@dane22](https://forums.plex.tv/u/dane22)\
**Post date:** [October 23, 2016, 9:19pm UTC](https://forums.plex.tv/t/how-to-debug-plex-add-in-code/164343/2 "2016-10-23T21:19:09Z")

</div>

If you find out, then do let me know 🙂

When I develop towards PMS, I launch ot from command line, and then the combo of log entries and print statements are my friend.

So sadly more a trial-error development ☹

---

<div class="post-metadata">

**Author:** ![shopgirl284](https://avatars.discourse-cdn.com/v4/letter/s/f6c823/32.png) [@shopgirl284](https://forums.plex.tv/u/shopgirl284)\
**Post date:** [October 24, 2016, 2:44pm UTC](https://forums.plex.tv/t/how-to-debug-plex-add-in-code/164343/3 "2016-10-24T14:44:25Z")

</div>

I cannot speak to any specific issues using a NAS for your PMS and how that would affect updating code and pulling logs. But the best method of debugging is adding log statements to your code. And then pull up the channel log (or system log if it is an issue with the URL service or playback) to see the data and determine where and what the issue may be.

For example:  
`try: my_variable = html.xpath('//@href')[0] except: Log('entered except statement') my_variable = None Log('the value of my_variable is %s' %my_variable)`

Also do not use Plex Web for testing your plugin code since you have to clear the cache to see your changes in real time. This discussion may also be helpful - [forums.plex.tv/discussion/comment/1133102/#Comment\_1133102](https://forums.plex.tv/discussion/comment/1133102/#Comment_1133102 "https://forums.plex.tv/discussion/comment/1133102/#Comment\_1133102")

---

<div class="post-metadata">

**Author:** ![czukowski](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/czukowski/32/9008_2.png) [@czukowski](https://forums.plex.tv/u/czukowski)\
**Post date:** [October 28, 2016, 11:35am UTC](https://forums.plex.tv/t/how-to-debug-plex-add-in-code/164343/4 "2016-10-28T11:35:18Z")

</div>

@baradanikto I routinely debug my Plex channel code, but only on the local machine since this is also where my Plex Server is. It is most probably possible to remote debug as well, but that would require to set up Python debugger at the box and connect to it. I don’t think it’s necessary though, because if you want to debug your own Python code, it shouldn’t matter much where you run it.

Here’s a package that you could add to your channel as a git submodule, it’ll help to set up testing runtime environment, create and run unit tests for the channel code. The practical point is that once you’re able to run any function in your channel, you can also debug it using a standard Python debugger.

[https://bitbucket.org/czukowski/plex-test-case/src](https://bitbucket.org/czukowski/plex-test-case/src)

---

<div class="post-metadata">

**Author:** ![baradanikto](https://avatars.discourse-cdn.com/v4/letter/b/5e9695/32.png) [@baradanikto](https://forums.plex.tv/u/baradanikto)\
**Post date:** [October 28, 2016, 1:57pm UTC](https://forums.plex.tv/t/how-to-debug-plex-add-in-code/164343/5 "2016-10-28T13:57:07Z")

</div>

czukowski - your answer sounds interesting. Can you explain further? What steps do you take to debug the code? My intent is to debug the code using Visual Studio’s Python add-in. I’m running on Win 7, so, I can easily start the PMS for Windows locally. I’m just not sure how to connect the debugger to the PMS.

---

<div class="post-metadata">

**Author:** ![czukowski](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/czukowski/32/9008_2.png) [@czukowski](https://forums.plex.tv/u/czukowski)\
**Post date:** [October 28, 2016, 3:22pm UTC](https://forums.plex.tv/t/how-to-debug-plex-add-in-code/164343/6 "2016-10-28T15:22:48Z")

</div>

The key is you don’t run your channel from Plex Server, but bootstrap the Framework directly and load your channel code into its sandbox environment. Then it’s just a matter of calling a function you need. There must be quite a lot of paths added to `PYTHONPATH` though, you’ll need to figure out how to do it from Visual Studio (I use PyCharm and it requires some quirks to make it work - see the readme from that repo I linked, though maybe VS will have it simpler), I use Python interpreter shipped with Plex (`PlexScriptingHost.exe` - it’s just good old `python.exe` renamed). That repo actually works as a base for unit tests as I mentioned, and does most of the Framework initialization phase for you, so as for running it, it’s just a matter of creating a class inheriting from the base `PlexTestCase`, adding a few test methods that calls the functions you need (and optionally asserting the results) and then configuring Run or Debug command on the tests runner that comes with IDE.

Edit: Now that said, this unit testing base is far from mature as I’ve only used it in a few channels to streamline testing of the web pages parsing functions, so some adjustments may still be needed depending on your use cases.

---

<div class="post-metadata">

**Author:** ![angeloavv](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/angeloavv/32/105985_2.png) [@angeloavv](https://forums.plex.tv/u/angeloavv)\
**Post date:** [November 18, 2018, 7:55am UTC](https://forums.plex.tv/t/how-to-debug-plex-add-in-code/164343/7 "2018-11-18T07:55:22Z")

</div>

@czukowski thank you for your deep explanation on how to setup a plex test environment. Do you think this could be extended in order to test plug-ins?

---

<div class="post-metadata">

**Author:** ![czukowski](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/czukowski/32/9008_2.png) [@czukowski](https://forums.plex.tv/u/czukowski)\
**Post date:** [November 18, 2018, 2:07pm UTC](https://forums.plex.tv/t/how-to-debug-plex-add-in-code/164343/8 "2018-11-18T14:07:23Z")

</div>

@angeloavv this was meant to be a platform for testing plug-ins. I have a channel that I haven’t finished, but it has some tests that could serve as an example, see the Tests directory:

[https://bitbucket.org/czukowski/plex-tvkultura/src/4522a9841a9c501b3f37bd1dfcb1a63f2cfb20bc/Contents/?at=master](https://bitbucket.org/czukowski/plex-tvkultura/src/4522a9841a9c501b3f37bd1dfcb1a63f2cfb20bc/Contents/?at=master)

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/plex/original/3X/2/a/2acb9765406f63293d357b4ec509ec39aa28f2ad.png) [@system](https://forums.plex.tv/u/system)\
**Post date:** [January 8, 2020, 8:00pm UTC](https://forums.plex.tv/t/how-to-debug-plex-add-in-code/164343/10 "2020-01-08T20:00:09Z")

</div>


