NOTICE: Account creation on the wiki has been temporarily disabled until the wiki is moved to OpenID. If you need an account before then, please request one here: http://forum.xbmc.org/showthread.php?tid=165868

HOW-TO:Debug Python Scripts with Eclipse

From XBMC
Jump to: navigation, search

Here is a method you can adopt in order to use the Eclipse (multi-platforms) remote debugger with your Python scripts/plugin.

Note: Follow this link Debugging Python Scripts on Linux with WinPDB for more information if you prefer to use WinPDB instead.

Note: The information provided herein is also applicable to user with Aptana Studio 3 installation which come pre-installed with pydev. For this case, you can skip to Configure Pydev and Eclipse after the Aptana Studio 3 installation.


Contents

1 Prerequisite

XBMC installed on Ubuntu (should work just fine on Windows and Mac too)



2 Prepare and Setup Eclipse

2.1 Install Eclipse

Note: in our example Eclipse Galileo (3.5.1) is used


2.2 Install Pydev in Eclipse

EclipsePydevUpdate.png

Note: in our example PyDev 1.5.5


2.3 Configure Pydev and Eclipse

2.3.1 Add Pyhton Interpreter Path

EclipsePydevPref.png

This will help in order to have Python code completion (except for XBMC modules)


2.3.2 Add Pydev view to Eclipse

EclipsePydevViewMenu.png

EclipsePydevViewMenu2.png Use this view for your developments in Python for XBMC


2.3.3 Add Pydev Start/Stop debug server buttons

EclipsePydevDebugSrvButtons.png


2.3.4 Setting the remote debugger port (optional)

If you do not want to use the default port (usually 5678) you can set it up:

2.3.5 Setting up Predefined Completions (optional)

Many developers are accustom to using predefined completions when writing their code (eg. type-ahead). The standard Python completions are enabled by default when using these instructions, but the standard XBMC components will not be recognized appropriately (xbmc, xbmcgui, xbmcplugin, etc).

In order to overcome this, you can run the PyDev Predefined Completions Creator addon and setup Eclipse to reference this file:

  1. Download the 'PyDev Predefined Completions Creator' Addon (eg. System > Settings > Addons > XBMC.org > Program Add-Ons)
  2. Navigate to the newly installed Addon (eg. Programs from the home screen)
  3. Open the context menu for newly installed addon and select 'Add-on Settings'
  4. Set the location to save the help docs to and save the new settings (This can be anywhere on your system - we'll configure it in Eclipse later)
  5. Select the add-on to run it (the addon should run and close quickly)
  6. Open Eclipse and select Window > Preferences > PyDev > Interpreter - Python
  7. From the 'Predefined' tab, click the new button and select the folder you created in the step above
  8. Press Apply / OK to ensure the changes are saved

3 Create a Python script/plugin for XBMC in Eclipse

3.1 Create a new PyDev Project

Most of the time when you are doing development, likely that you are running XBMC and Eclipse on the same platform; (The installed debugger described in this article actually can also work with a remote XBMC).
Let's say you already have scripts installed in XBMC script folder that you want to create a new PyDev project e.g.

C:\XBMC\addons\myscript1
C:\XBMC\addons\myscript2

With myscript1 & myscript2 being sub-directories of C:\XBMC\addons each containing the Default.py scripts.
First you need to define the Python project workspace:

Then proceed to create the Python project:

OR select below in case PyDev Project is not displayed


3.2 Set the Python Path


3.3 Import File System

Assuming you want to add in all the directories under C:\XBMC\addons

Your source code is now recognized by Eclipse and you will have access to neat functionality such as code completion, link between source files, etc.


3.4 Add Pydev Python source code (pysrc) to XBMC

XBMC needs to know where to find the PyDev remote python source file in order to communicate with the remote debugger of Eclipse.

3.4.1 Identify where PyDev remote debugger Python files are

Python source files should be in you Eclipse installation directory:

C:\...\eclipse\plugins\org.python.pydev.debug_1.5.5.2010030420\pysrc\

In our case we use PyDev 1.5.5. Look for org.python.pydev.debug (not core)

3.4.2 Modify PyDev source in order to support XBMC paths

The pysrc code that came with Eclipse-pydev does not support XBMC paths (special://... or Q:/...) and the module would not find them making it impossible to insert breakpoints in such files.

In order to add the support of XBMC paths, you will need to do a simple modification to the file pydevd_file_utils.py consisting on adding a line to each procedure that had a filename parameter (4 procedures in total). Add a line like


def _NormFile(filename):
    filename = xbmc.translatePath(filename)
    try:
...

The other three procedures are:

of course you need the import directive at the top of the file:

import xbmc

at the beginning of the module...

3.4.3 Choose how XBMC will refer to Pydev debugging module

You have 3 options as below:

I am only going to show you the steps for Option 3:

3.4.4 Copy pysrc in XBMC Python install

XBMC\system\python\Lib\pysrc\

3.5 Add to your python script the code for remote debug

At the beginning of you Python script/plugin (usually Default.py) add the following code:

REMOTE_DBG = True 

# append pydev remote debugger
if REMOTE_DBG:
    # Make pydev debugger works for auto reload.
    # Note pydevd module need to be copied in XBMC\system\python\Lib\pysrc
    try:
        import pysrc.pydevd as pydevd
	# stdoutToServer and stderrToServer redirect stdout and stderr to eclipse console
        pydevd.settrace('localhost', stdoutToServer=True, stderrToServer=True)
    except ImportError:
        sys.stderr.write("Error: " +
            "You must add org.python.pydev.debug.pysrc to your PYTHONPATH.")
        sys.exit(1)

In the case you have change the default port you can replace the line with pydevd.settrace by this one:

        pydevd.settrace('localhost', port=5678, stdoutToServer=True, stderrToServer=True)


3.6 Run

Debug Server at port: 5678

3.7 Screenshot

EclipsePydevDebugger.png

4 See also

Development:

Personal tools
Namespaces
Variants
Actions
Navigation
Wiki help
Google Search
Toolbox