HOW-TO:Debug Python Scripts with Eclipse

HOW-TO:DebugPython Scripts with Eclipse

From XBMC

Jump to: navigation,search

Here is amethod you can use in order to use the Eclipse (multi-platforms) remotedebugger with you Python scripts/plugin.

Note: you canmore information if you prefer to use WinPDB instead: Debugging Python Scripts on Linux withWinPDB

Contents

HOW-TO:Debug Python Scripts with Eclipse. 1

Prerequisite. 1

Prepare and Setup Eclipse. 1

Install Eclipse. 1

Install Pydev in Eclipse. 1

Configure Pydev and Eclipse. 2

Create a Python script/plugin for XBMC in Eclipse. 4

Create a new PyDev Project. 4

Set the Python Path. 5

Add Pydev Python source code (pysrc) to XBMC.. 5

Add to your python script the code for remote debug. 6

Run. 7

Screenshot. 8

 

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 installedon 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 ourexample 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:

                             

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

Note: in ourexample 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:

This will helpin 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

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

Use this viewfor 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

 

Setting the remote debugger port (optional)

If you do notwant 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 XBMCin Eclipse

Create a new PyDev Project

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

  • File ->     New -> PyDev Project

or PyDevProject 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 myscriptbeing 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 sourcecode is now recognize by Eclipse and you will have access to neatfunctionnalities such as code completion, link between source files, etc.

 

Add Pydev Python source code (pysrc) toXBMC

XBMC needs toknow where to find the PyDev remote python source file in order to communicatewith the remote debugger of Eclipse.

Identify where PyDev remote debugger Python files are

Python sourcefiles should be in you Eclipse installation directory:

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

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

Modify PyDev source in order to support XBMC paths

The pysrc codethat came with Eclipse-pydev does not support XBMC paths (special://... orQ:/...) and the module would not find them making it impossible to insertbreakpoints in such files.

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

 

def _NormFile(filename):

   filename = xbmc.translatePath(filename)

   try:

...

of course youneed a

import xbmc

at thebeginning of the module...

Choose how XBMC will refer to Pydev debugging module

Now You haveseveral 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 goingto 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 forremote debug

At thebeginning of you Python script/plugin (usaully Default.py) add the followingcode:

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 addorg.python.pydev.debug.pysrc to your PYTHONPATH.")

       sys.exit(1)

In the case youhave change the default port you can replace the line with pydevd.settrace bythis 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

  • 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!

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值