Stealth Client Forum

Альтернативный клиент Ultima Online
Текущее время: 17.11.2017 23:04

Часовой пояс: UTC + 2 часа




Начать новую тему Ответить на тему  [ Сообщений: 3 ] 
Автор Сообщение
 Заголовок сообщения: How to debug Python Scripts
СообщениеДобавлено: 19.02.2012 21:11 
Не в сети Neophyte
Neophyte

Зарегистрирован: 12.02.2012 18:06
Сообщения: 36
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, but in english (I don't speak 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). 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 ;)).

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

  1. From Eclipse menu choose Help -> Install New Software...

    Изображение
    View Screen Capture
  2. Click on the "Add" button to add a the PyDev site;

    Изображение
    View Screen Capture
  3. In the new prompt that appears type the name you like (I used "PyDev") and as site use the official site for PyDev releases: http://pydev.org/updates

    Изображение
    View Screen Capture
  4. Now you are ready to install PyDev. You don't need Mylyn itegration (see the screen below).

    Изображение
    View Screen Capture

Configuring PyDev

Add the Python interpreter
  1. From the menu choose Window -> Preferences

    Изображение
    View Screen Capture
  2. 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

  1. On the top right corner click on the Open perspective button and select Other

    Изображение
    View Screen Capture
  2. Select PyDev and confirm

    Изображение
    View Screen Capture

Add the remote debugger start and stop buttons

  1. 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
  2. In the "Commands Group Availability" tab put the check on the PyDev Debug option;

    Изображение
    View Screen Capture
  3. Now you should see the remote debugging buttons in you toolbar:

    Изображение
    View Screen Capture
  4. 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

  1. 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.
  2. 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;
  3. 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!):
Код:
REMOTE_DBG = True

if REMOTE_DBG:
   sys.path.append('D:\\stealth\\script\\pydebug')
   print str(sys.path)
   import pysrc.pydevd as pydevd
   pydevd.settrace('localhost', stdoutToServer=True, stderrToServer=True)

Код:
REMOTE_DBG = True

Here you define a Boolean variable to be able to switch the debugger as you like (see the if condition soon after after)

Код:
sys.path.append('D:\\stealth\\script\\pydebug')

This line is really important. It is the line that tells to Python where to look for the pysrc module that we have created in the step before. As you can see it 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)
Код:
import pysrc.pydevd as pydevd

Here 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.
Код:
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:
  1. From the menu choose Window -> Preferences

    Изображение
    View Screen Capture
  2. Expand the PyDev tree and choose Debug -> Source Locator; select the last reference you used and remove it.

    Изображение
    View Screen Capture

Credits and references

References
This guide has been inspired from the following pages:

Credits
Thank to Alex that in this post gave me the initial tips on how to start.


Последний раз редактировалось Boydon 19.02.2012 23:44, всего редактировалось 2 раз(а).

Вернуться к началу
 Профиль  
Ответить с цитатой  
 Заголовок сообщения: Re: How to debug Python Scripts
СообщениеДобавлено: 19.02.2012 22:57 
Не в сети Developer
Developer

Зарегистрирован: 24.03.2005 17:05
Сообщения: 3637
oh. great job, Boydon!

_________________
"Пишите код так, как будто сопровождать его будет склонный к насилию психопат, который знает, где вы живете". (с) Макконнелл, "Совершенный код".


Вернуться к началу
 Профиль  
Ответить с цитатой  
 Заголовок сообщения: Re: How to debug Python Scripts
СообщениеДобавлено: 24.11.2012 6:54 
Не в сети Neophyte
Neophyte

Зарегистрирован: 20.11.2012 11:20
Сообщения: 23
Thanks for ur share. Nice Boydon!


Вернуться к началу
 Профиль  
Ответить с цитатой  
Показать сообщения за:  Поле сортировки  
Начать новую тему Ответить на тему  [ Сообщений: 3 ] 

Часовой пояс: UTC + 2 часа


Кто сейчас на конференции

Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 1


Вы не можете начинать темы
Вы не можете отвечать на сообщения
Вы не можете редактировать свои сообщения
Вы не можете удалять свои сообщения
Вы не можете добавлять вложения

Найти:
Перейти:  
cron


Тюнинг и аксессуары. Тюнинг внедорожников: магазин 4х4 в Одессе. Интернет магазин "AMAX".
Также, на правах рекламы: школьный сайт: school-work.net и еще один: https://www.youtube.com/channel/UCFSPHKHjP22tEl8Rjcst9Fw.


Рейтинг@Mail.ru
Powered by phpBB® Forum Software © phpBB Group