Author Topic: How to debug Python Scripts  (Read 14841 times)

0 Members and 1 Guest are viewing this topic.

Offline BoydonTopic starter

  • Moderator
  • **
  • *****
  • Posts: 76
  • Activity:
    0%
  • Reputation Power: 3
  • Boydon has no influence.
  • Respect: +16
  • Referrals: 0
    • View Profile
How to debug Python Scripts
« on: February 19, 2012, 10:24:10 AM »
0
Hello everyone,

I'm going to show how you can debug yours Stealth scripts written in Python using Pydev.
If you have any question or something is not clear please let me know.

This guide has also been posted on the official stealth board, but I'm posting it here cause on their board the majority of user-base is russian.

Prerequisites

  • Any PC able to run the Eclipse IDE (this is the platform on the top of witch we'll run our debugger);
  • The Python environment compatible with Stealth (for this tutorial I've used 2.7.x, but this has also been tested and proved to work with 3.2.x with some small changes). If you're reading this probably you know what I'm speaking of :D;
  • Pydev extension for Eclipse (this is where the sweet part of debug takes place ;)). At the moment of writing I'm using 2.4 version.
Preparation Steps

Installing Eclipse

Download a version of Eclipse that suits your PC and install it (for this tutorial I've used the Eclipse IDE for Java EE Developers).

Installing PyDev on top of Eclipse


Configuring PyDev

Add the Python interpreter
  • From the menu choose Window -> Preferences


    View Screen Capture
  • Expand the PyDev tree (step 1), choose "Interpreter Python" (step 2), click on the "New" Button (step 3) and point Eclipse to your python.exe path (on my machine as you can see is "D:\Python27\python.exe", the screen has been taken after configuring so you see the result of it).


    View Screen Capture

Add the PyDev perspective to Eclipse


Add the remote debugger start and stop buttons

  • Be sure to be in PyDev perspective and click on a empty spot on you toolbar and from the menu choose "Customize Perspective...";


    View Screen Capture
  • In the "Commands Group Availability" tab put the check on the PyDev Debug option;


    View Screen Capture
  • Now you should see the remote debugging buttons in you toolbar:


    View Screen Capture
  • Click on the green one and start the remote debugger (you can be sure that the debugger is started checking at the bottom of Eclipse interface in the Console log).


    View Screen Capture

Make Stealth interact with PyDev remote debugger

Now that everything is set up in PyDev we need to address Stealth to interact with it.

Referencing the pysrc module inside Stealth installation

  • The first thing to make here is to find where the original pysrc module (bundled inside PyDev) has bee installed on our machine. The path changes depending on the installation. Typically it is in a sub folder of the Eclipse installation; in my machine it was ...\eclipse\plugins\org.python.pydev.debug_2.3.0.2011121518\pysrc on your machine should be something very similar.
  • Once you've found it make a copy of the whole pysrc folder and place it wherever you like it. On my machine i putted it in the following path "D:\stealth\script\pydebug\pysrc" being "D:\stealth\" the path where my Stealth installation resides;
  • Now go inside the newly created directory ("D:\stealth\script\pydebug\pysrc") and create a new empty file and name it "__init__.py".

Call the Remote Debugger from within you code

Now everything is ready and you only need to make your script aware of the remote debugger. To make this happen add this snippet in the header of your Python script (I'm going to comment it line by line later on) before launching it from Stealth (not PyDev!):
Code: [Select]
REMOTE_DBG = True

if REMOTE_DBG:
sys.path.append('D:\\stealth\\script\\pydebug')
sys.path.append('D:\\stealth\\script\\pydebug\\pysrc') #this is only needed if you run Python >= 3.2.x
print str(sys.path)
import pysrc.pydevd as pydevd
pydevd.settrace('localhost', stdoutToServer=True, stderrToServer=True)
Code: [Select]
REMOTE_DBG = TrueHere you define a Boolean variable to be able to switch the debugger as you like (see the if condition soon after after)

Code: [Select]
sys.path.append('D:\\stealth\\script\\pydebug')
sys.path.append('D:\\stealth\\script\\pydebug\\pysrc') #this is only needed if you run Python >= 3.2.x
Those two lines are really important. They are the lines that tells to Python where to look for the pysrc module that we have created in the step before. As you can see (the first of the two lines) is the parent directory of the one where we copied the pysrc module "D:\stealth\script\pydebug\pysrc" and the slash characters are escaped. To be sure that the path has been correctly added we also issue a print statement (you can never be sure... :P). If you are using Python 3.2.x you also need to explicitly specify the pysrc folder; this is due to differences in the way that Python 3.x handle the imports.
Code: [Select]
import pysrc.pydevd as pydevdHere you import the pydevd class from the pysrc module. If you have an error here you did not added the pysrc path correctly in the previous step.
Code: [Select]
pydevd.settrace('localhost', stdoutToServer=True, stderrToServer=True)This line will try to connect Stealth to the remote debugger (be sure to have started it) so you can start debugging. If everything is set up correctly you'll be prompted for where to find the script to debug: point it to you current script folder, swith to the Debug Perspective and you are done. :)

Known Issues
PyDev, will only promt you one time asking where to look for debug source and then will save the location and use it for following debug sessions. This is good if you are debugging the same script, but if you want to debug another script you'll have to make the following steps:

Credits and references

References
This guide has been inspired from the following pages:

Credits
Thank to Alex (from the Stealth comunity) that in this post gave me the initial tips on how to start.
Thanks to Crome969 that pointed out troubles with Python 3.2.x
« Last Edit: March 05, 2012, 05:07:12 AM by Boydon »
Member of the Stealth development team.

Offline TrailMyx

  • Officially retired from UO
  • Administrator
  • *
  • *
  • Posts: 13301
  • Activity:
    0%
  • Reputation Power: 154
  • TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!
  • Gender: Male
  • Viper!
  • Respect: +1349
  • Referrals: 33
    • View Profile
    • ScriptUO
Re: How to debug Python Scripts
« Reply #1 on: February 19, 2012, 10:39:06 AM »
0
Holy crap, Boydon.  Great writeup!!  Thanks for that.  +rep for you.
Please read the ScriptUO site RULES
Come play RIFT with me!

Offline Crome969

  • Moderator
  • *
  • *****
  • Posts: 2098
  • Activity:
    0%
  • Reputation Power: 25
  • Crome969 is on the verge of being accepted.Crome969 is on the verge of being accepted.Crome969 is on the verge of being accepted.Crome969 is on the verge of being accepted.Crome969 is on the verge of being accepted.
  • Gender: Male
  • UO Enthusiast
  • Respect: +211
  • Referrals: 10
    • View Profile
    • ScriptSDK
Re: How to debug Python Scripts
« Reply #2 on: February 19, 2012, 11:54:04 AM »
0
Thank you Boydon,
you pushed that way really good. Tutorial is simple to understand i will use this for my python debug :)

Offline camotbik

  • Sr. Member
  • *
  • Posts: 349
  • Activity:
    0%
  • Reputation Power: 3
  • camotbik has no influence.
  • Gender: Male
  • Hello! I'm a UO addict.
  • Respect: +38
  • Referrals: 0
    • View Profile
Re: How to debug Python Scripts
« Reply #3 on: February 29, 2012, 07:23:35 PM »
0
Great job Boydon, it's a shame i'm more of a pascal guy )
What you witness -- is whatver..
uogamers hybrid.

Offline BoydonTopic starter

  • Moderator
  • **
  • *****
  • Posts: 76
  • Activity:
    0%
  • Reputation Power: 3
  • Boydon has no influence.
  • Respect: +16
  • Referrals: 0
    • View Profile
Re: How to debug Python Scripts
« Reply #4 on: March 05, 2012, 05:08:57 AM »
0
Updated the guide to support Python 3.2.x. Thanks to Crome969 that spotted out trobles. :)
Member of the Stealth development team.

Offline TrailMyx

  • Officially retired from UO
  • Administrator
  • *
  • *
  • Posts: 13301
  • Activity:
    0%
  • Reputation Power: 154
  • TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!
  • Gender: Male
  • Viper!
  • Respect: +1349
  • Referrals: 33
    • View Profile
    • ScriptUO
Re: How to debug Python Scripts
« Reply #5 on: March 06, 2012, 08:00:34 PM »
0
I dunno, this is pushing my closer and closer to giving this thing a whirl!
Please read the ScriptUO site RULES
Come play RIFT with me!

Offline dxrom

  • Master of the milestones!
  • Elite
  • *
  • *
  • Posts: 1080
  • Activity:
    0%
  • Reputation Power: 15
  • dxrom is working their way up.dxrom is working their way up.dxrom is working their way up.
  • KEYBOARD COWBOY, GREAT SAMURAI OF THE INTERNET.
  • Respect: +100
  • Referrals: 1
    • View Profile
Re: How to debug Python Scripts
« Reply #6 on: May 14, 2013, 07:18:41 AM »
0
Just curious...

Failed: Python Interpreter not found. Install Python x86(2.7 or 3.3) from http://python.org/download/

And for what it's worth. I was never prompted what to add here:
« Last Edit: May 14, 2013, 07:33:10 AM by dxrom »



 ​_██​_
(ಠ​_ృ)
I do say, ol' Chap! Come play EVE Online! Why here is a 21 Day Free Trial!

Offline BoydonTopic starter

  • Moderator
  • **
  • *****
  • Posts: 76
  • Activity:
    0%
  • Reputation Power: 3
  • Boydon has no influence.
  • Respect: +16
  • Referrals: 0
    • View Profile
Re: How to debug Python Scripts
« Reply #7 on: June 09, 2013, 08:29:19 AM »
0
Be sure to have a Python interpreter installed as specified in step:

Add the Python interpreter

Quote from: Boydon
Window -> Preferences
 
Expand the PyDev tree (step 1), choose "Interpreter Python" (step 2), click on the "New" Button (step 3) and point Eclipse to your python.exe path (on my machine as you can see is "D:\Python27\python.exe", the screen has been taken after configuring so you see the result of it).
Member of the Stealth development team.

Offline dxrom

  • Master of the milestones!
  • Elite
  • *
  • *
  • Posts: 1080
  • Activity:
    0%
  • Reputation Power: 15
  • dxrom is working their way up.dxrom is working their way up.dxrom is working their way up.
  • KEYBOARD COWBOY, GREAT SAMURAI OF THE INTERNET.
  • Respect: +100
  • Referrals: 1
    • View Profile
Re: How to debug Python Scripts
« Reply #8 on: June 09, 2013, 02:03:44 PM »
0
Be sure to have a Python interpreter installed as specified in step:

Add the Python interpreter

Quote from: Boydon
Window -> Preferences
 
Expand the PyDev tree (step 1), choose "Interpreter Python" (step 2), click on the "New" Button (step 3) and point Eclipse to your python.exe path (on my machine as you can see is "D:\Python27\python.exe", the screen has been taken after configuring so you see the result of it).

All that was setup already, anyways I gave up on it a long time ago :>



 ​_██​_
(ಠ​_ృ)
I do say, ol' Chap! Come play EVE Online! Why here is a 21 Day Free Trial!

Offline blackbeard

  • Restricted
  • **
  • Posts: 10
  • Activity:
    0%
  • Reputation Power: 1
  • blackbeard has no influence.
  • Respect: 0
  • Referrals: 0
    • View Profile
Re: How to debug Python Scripts
« Reply #9 on: February 10, 2018, 02:27:37 AM »
0
: I`m following this tutorial but getting an error when run this

Code: [Select]
import sys
REMOTE_DBG = True

if REMOTE_DBG:
    sys.path.append('D:\\Jogos\\Ultima Online\\Stealth\\Stealth_v8.7.2\\Scripts\\pydebug')
    sys.path.append('D:\\Jogos\\Ultima Online\\Stealth\\Stealth_v8.7.2\\Scripts\\pydebug\\pysrc') #this is only needed if you run Python >= 3.2.x
    print str(sys.path)
    import pysrc.pydevd as pydevd
    pydevd.settrace('localhost', stdoutToServer=True, stderrToServer=True)

AttributeError: 'module' object has no attribute 'declare_namespace'

Code: [Select]
09:26:14:450 [tfg noob]: ['D:\\Jogos\\Ultima Online\\Stealth\\Stealth_v8.7.2\\py_stealth', 'C:\\WINDOWS\\SYSTEM32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages', 'C:\\Python27\\lib\\site-packages\\win32', 'C:\\Python27\\lib\\site-packages\\win32\\lib', 'C:\\Python27\\lib\\site-packages\\Pythonwin', 'D:\\Jogos\\Ultima Online\\Stealth\\Stealth_v8.7.2', 'D:\\Jogos\\Ultima Online\\Stealth\\Stealth_v8.7.2\\Scripts\\workspace\\UO-001\\src\\core', 'D:\\Jogos\\Ultima Online\\Stealth\\Stealth_v8.7.2\\Scripts\\pydebug', 'D:\\Jogos\\Ultima Online\\Stealth\\Stealth_v8.7.2\\Scripts\\pydebug\\pysrc']
09:26:14:450 [tfg noob]:
09:26:14:749 [tfg noob]: Traceback (most recent call last):
09:26:14:749 [tfg noob]:   File "D:\Jogos\Ultima Online\Stealth\Stealth_v8.7.2\Scripts\workspace\UO-001\src\core\auto2.py", line 8, in <module>
09:26:14:749 [tfg noob]:     import pysrc.pydevd as pydevd
09:26:14:749 [tfg noob]:   File "D:\Jogos\Ultima Online\Stealth\Stealth_v8.7.2\Scripts\pydebug\pysrc\pydevd.py", line 28, in <module>
09:26:14:749 [tfg noob]:     from _pydevd_bundle import pydevd_vars
09:26:14:749 [tfg noob]:   File "D:\Jogos\Ultima Online\Stealth\Stealth_v8.7.2\Scripts\pydebug\pysrc\_pydevd_bundle\pydevd_vars.py", line 8, in <module>
09:26:14:749 [tfg noob]:     from _pydevd_bundle.pydevd_xml import ExceptionOnEvaluate, get_type, var_to_xml
09:26:14:749 [tfg noob]:   File "D:\Jogos\Ultima Online\Stealth\Stealth_v8.7.2\Scripts\pydebug\pysrc\_pydevd_bundle\pydevd_xml.py", line 3, in <module>
09:26:14:749 [tfg noob]:     from _pydevd_bundle import pydevd_extension_utils
09:26:14:750 [tfg noob]:   File "D:\Jogos\Ultima Online\Stealth\Stealth_v8.7.2\Scripts\pydebug\pysrc\_pydevd_bundle\pydevd_extension_utils.py", line 4, in <module>
09:26:14:750 [tfg noob]:     import pydevd_plugins.extensions
09:26:14:750 [tfg noob]:   File "D:\Jogos\Ultima Online\Stealth\Stealth_v8.7.2\Scripts\pydebug\pysrc\pydevd_plugins\extensions\__init__.py", line 2, in <module>
09:26:14:750 [tfg noob]:     __import__('pkg_resources').declare_namespace(__name__)
09:26:14:750 [tfg noob]: AttributeError: 'module' object has no attribute 'declare_namespace'

using pydev 6.2.0
eclipse 4.6
python 2.7.14

Tags: