Archive:Debug Python Scripts with Eclipse: Difference between revisions

From Official Kodi Wiki
Jump to navigation Jump to search
>Temhil
No edit summary
>Temhil
(1st final version of the page)
Line 2: Line 2:


Note: you can more information if you prefer to use WinPDB instead: [[HOW-TO_debug_Python_Scripts|Debugging Python Scripts on Linux with WinPDB]]
Note: you can more information if you prefer to use WinPDB instead: [[HOW-TO_debug_Python_Scripts|Debugging Python Scripts on Linux with WinPDB]]


==Prerequisite==
==Prerequisite==
Line 9: Line 11:
XBMC installed on Ubuntu (should work just fine on Windows and Mac too)
XBMC installed on Ubuntu (should work just fine on Windows and Mac too)
* [http://pydev.org/ Pydev ] plugin for Eclipse IDE: Pydev is a Python IDE for Eclipse, which may be used in Python, Jython and IronPython development.
* [http://pydev.org/ Pydev ] plugin for Eclipse IDE: Pydev is a Python IDE for Eclipse, which may be used in Python, Jython and IronPython development.


==Prepare and Setup Eclipse==
==Prepare and Setup Eclipse==
===Install Eclipse===
===Install Eclipse===
* Download  [http://www.eclipse.org/downloads/ Eclipse IDE] and install it
* Download  [http://www.eclipse.org/downloads/ Eclipse IDE] and install it
Note: in our example Eclipse Galileo (3.5.1) is used
Note: in our example Eclipse Galileo (3.5.1) is used


===Install Pydev in Eclipse===
===Install Pydev in Eclipse===
Line 22: Line 29:
* Check PyDev box and follow the steps in order to install PyDev
* Check PyDev box and follow the steps in order to install PyDev
Note: in our example PyDev 1.5.5
Note: in our example PyDev 1.5.5


===Configure Pydev and Eclipse===
===Configure Pydev and Eclipse===
====Add Pyhton Interpreter Path====
====Add Pyhton Interpreter Path====
* Go in Window -> Preferences
* Go in Window -> Preferences
Line 30: Line 41:
[[File:EclipsePydevPref.png]]
[[File:EclipsePydevPref.png]]
This will help in order to have Python code completion (except for XBMC modules)
This will help in order to have Python code completion (except for XBMC modules)
====Add Pydev view to Eclipse====
====Add Pydev view to Eclipse====
* On the top right corner clic on the View button on select Others
* On the top right corner clic on the View button on select Others
Line 36: Line 49:
[[File:EclipsePydevViewMenu2.png]]
[[File:EclipsePydevViewMenu2.png]]
Use this view for your developments in Python for XBMC
Use this view for your developments in Python for XBMC
====Add Pydev Start/Stop debug server buttons====
====Add Pydev Start/Stop debug server buttons====
* Right clic on the toolbar
* Right clic on the toolbar
Line 43: Line 58:
* You should see 2 new buttons in your toolbar
* You should see 2 new buttons in your toolbar
[[File:EclipsePydevDebugSrvButtons.png]]
[[File:EclipsePydevDebugSrvButtons.png]]
====Setting the remote debugger port (optional)====
====Setting the remote debugger port (optional)====
If you do not want to use the default port (usually 5678) you can set it up:
If you do not want to use the default port (usually 5678) you can set it up:
* Go in Window -> Preferences
* Go in Window -> Preferences
* Pydev then Debug: you can define here the port and the timeout you want
* Pydev then Debug: you can define here the port and the timeout you want




==Create a Python script/plugin for XBMC in Eclipse==
==Create a Python script/plugin for XBMC in Eclipse==
Most of the time
 
 
===Create a new PyDev Project===
Most of the time when you develop you run XBMC and Eclipse on the same platform (but this debugger work with a remote XBMC too).
Let say you already have a script installed in XBMC script folder
In order to create a new PyDev project:
* File -> New -> PyDev Project
or PyDev Project is not displayed
* File -> New -> Others and Select Pydev then PyDev Project
* Then clic next
* Set the name of your project
* Uncheck Use Default
* Browse for the directory of you script where XBMC is installed for example:
<pre>C:\XBMC\scripts\myscript</pre> with myscript being a directory containing the Default.py script
* Set Python as Project Type
* Select 2.4 (XBMC Python version) for the grammar
* Uncheck Create default 'src' folder ...
* Clic on Finish
 
 
===Set the Python Path===
* Right clic on your new project and select properties
* Select PyDev - PYTHONPATH and clic on Add source folder
* Select the source directory of your script (or plugin), myscript in our example
Your source code is now recognize by Eclipse and you will have access to neat functionnalities such as code completion, link between source files, etc.
 
 
===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.
 
====Identify where PyDev remote debugger Python files are====
Python source files should be in you Eclipse installation directory:
<pre>C:\...\eclipse\plugins\org.python.pydev.debug_1.5.5.2010030420\pysrc\</pre>
In our case we use PyDev 1.5.5.
Look for org.python.pydev.debug (not core)
 
You have several options:
* Option 1 - Refer from our python script direclty the directory where pysrc is installed
* Option 2 - Copy pysrc in our script directory and refer it from our script
* Option 3 - Copy pysrc in XBMC Python install
 
I am only going to show you the last option:
 
====Copy pysrc in XBMC Python install====
* Copy the content of to pysrc to:
<pre>XBMC\system\python\Lib\pysrc\</pre>
* Add an empty __init__.py file in it
 
===Add to your python script the code for remote debug===
At the beginning of you Python script/plugin (usaully Default.py) add the following code:
<pre>
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)
</pre>
 
In the case you have change the default port you can replace the line with pydevd.settrace by this one:
<pre>
        pydevd.settrace('localhost', port=5678, stdoutToServer=True, stderrToServer=True)
</pre>
 
* Use REMOTE_DBG in order to activate or not the remote debugging
* port specifies the port number use between the client and the server
* Replace localhost by an IP address if XBMC and Eclipse are on 2 different machines
* stdoutToServer and stderrToServer redirect stdout and stderr to eclipse console, you won't have to open xbmc.log file
 
 
 
===Run===
* Go in Debug view and press on the start server button: [[File:EclipsePydevDebugSrvButtons.png]]
 
* You will see in the console logs like:
<pre>Debug Server at port: 5678</pre>
 
* Start XBMC and run your script/plugin, you will see it as "Running" but paused
 
* In Eclipse (Debug View) clic on the Resume button (or F8), then you will see the program running in XBMC
 
* You are now fully set up: You can set breakpoint, watch on variables, run step by step etc
 
* Enjoy!

Revision as of 00:32, 4 April 2010

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

Note: you can more information if you prefer to use WinPDB instead: Debugging Python Scripts on Linux with WinPDB


Prerequisite

  • Any platform able to run Eclipse IDE (Windows 32bit, Mac Carbon 32bit-64bit, Mac Cocoa 32bit-64bit, Linux 32bit-64bit): it is the platform from where you will run the debugger IDE
  • Pyhton interpreter 2.4 (same version as XBMC) install on the machine running Eclipse
  • Any platform running XBMC (Xbox, Linux, MACOSX, Windows): it is the platform running Python scripts/plugin connecting with the remote debugger.

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

  • Pydev plugin for Eclipse IDE: Pydev is a Python IDE for Eclipse, which may be used in Python, Jython and IronPython development.


Prepare and Setup Eclipse

Install Eclipse

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


Install Pydev in Eclipse

  • In Eclipse go in: Help -> Install New Software...
  • Clic on the Add button and add Pydev website as a source: http://pydev.org/updates
  • You will see the window below:

EclipsePydevUpdate.png

  • Check PyDev box and follow the steps in order to install PyDev

Note: in our example PyDev 1.5.5


Configure Pydev and Eclipse

Add Pyhton Interpreter Path

  • Go in Window -> Preferences
  • Pydev then Interpreter Python
  • Clic on Add and Browse for your Python Interpreter 2.4, you should see something like that:

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


Add Pydev view to Eclipse

  • On the top right corner clic on the View button on select Others

EclipsePydevViewMenu.png

  • Select Pydev then OK, a PyDev View Button should be now available on the top right corner

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


Add Pydev Start/Stop debug server buttons

  • Right clic on the toolbar
  • Select customize perspective..., a window customize perspective will open
  • Select Command Groups Availability tab
  • Check Pydev Debug box and OK
  • You should see 2 new buttons in your toolbar

EclipsePydevDebugSrvButtons.png


Setting the remote debugger port (optional)

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

  • Go in Window -> Preferences
  • Pydev then Debug: you can define here the port and the timeout you want


Create a Python script/plugin for XBMC in Eclipse

Create a new PyDev Project

Most of the time when you develop you run XBMC and Eclipse on the same platform (but this debugger work with a remote XBMC too). Let say you already have a script installed in XBMC script folder In order to create a new PyDev project:

  • File -> New -> PyDev Project

or PyDev Project is not displayed

  • File -> New -> Others and Select Pydev then PyDev Project
  • Then clic next
  • Set the name of your project
  • Uncheck Use Default
  • Browse for the directory of you script where XBMC is installed for example:
C:\XBMC\scripts\myscript

with myscript being a directory containing the Default.py script

  • Set Python as Project Type
  • Select 2.4 (XBMC Python version) for the grammar
  • Uncheck Create default 'src' folder ...
  • Clic on Finish


Set the Python Path

  • Right clic on your new project and select properties
  • Select PyDev - PYTHONPATH and clic on Add source folder
  • Select the source directory of your script (or plugin), myscript in our example

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


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.

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)

You have several options:

  • Option 1 - Refer from our python script direclty the directory where pysrc is installed
  • Option 2 - Copy pysrc in our script directory and refer it from our script
  • Option 3 - Copy pysrc in XBMC Python install

I am only going to show you the last option:

Copy pysrc in XBMC Python install

  • Copy the content of to pysrc to:
XBMC\system\python\Lib\pysrc\
  • Add an empty __init__.py file in it

Add to your python script the code for remote debug

At the beginning of you Python script/plugin (usaully 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)
  • Use REMOTE_DBG in order to activate or not the remote debugging
  • port specifies the port number use between the client and the server
  • Replace localhost by an IP address if XBMC and Eclipse are on 2 different machines
  • stdoutToServer and stderrToServer redirect stdout and stderr to eclipse console, you won't have to open xbmc.log file


Run

  • Go in Debug view and press on the start server button: EclipsePydevDebugSrvButtons.png
  • You will see in the console logs like:
Debug Server at port: 5678
  • Start XBMC and run your script/plugin, you will see it as "Running" but paused
  • In Eclipse (Debug View) clic on the Resume button (or F8), then you will see the program running in XBMC
  • You are now fully set up: You can set breakpoint, watch on variables, run step by step etc
  • Enjoy!