Vous êtes sur la page 1sur 8

Using HAP debugger HAP debugger is an IDE tool for use with python scripting language.

Besides the basic IDE functionality it also allows for a high level debugging process which can be done locally as well as remotely. For the purpose of this tutorial we will concentrate on the usage for local development and debug. To simplify things we will break our tutorial into several steps. 1. Preparation Ensure that the computer you will be running HAP debugger on has the corresponding python interpreter version. (The HAP debugger application is linked against a single version of python interpreter, and for its proper operation it requires that the corresponding pythonXX.dll is in the System32 folder of your windows installation. The most likely scenario is that you will be working with the python version 2.3 and thus the HAP debugger you will use is the one linked against python23.dll, if this is the case, please copy the python23.dll manually from HAPs folder to the system32 folder, as there are problems with the original pythons dll.) Another thing you need to ensure is that your system has the PYTHON_ROOT environment variable defined. To achieve this do the following: Right click your My Computer icon on your desktop and select Properties, switch to the Advanced tab and click on the Environment Variables button. You will get a window looking similar to the following:

Scroll the bottom list to make sure that this variable isnt already defined and if it is, ensure that the path it points to is the path to the folder of your python installation. If the variable isnt present, click on the New button under the System variables section and fill the field as follows:

After this press OK three times and youll be all set up to use the HAP debugger

2. First run The HAPs gui is designed to be intuitive and as such uses the standard File type menu, so there seems to be no need to explain this interface further. What you should note is that the display is split into 3 main sections: - Project content display ( shows all the files which are part of the current project)

File content display (shows the content of the currently opened file, use CTRL+Tab to switch between multiple open files, it supports both maximized and cascade view)

console output display (shows the console output which is generated during the execution of scripts)

3. First project Now that you are somewhat familiar with HAPs user interface, we can start with our first project. First click on File/New Project, and a file open dialog will appear, select a folder for your testing purposes or create a new one (best use D: drive) and fill out the File name field with something appropriate and click Open

The following message will appear:

Seeing how our project is from scratch and that our project_folder still has no file/folder structure to be used as initial project structure, you can freely click No here. Now you will see Untitled project in your project content display.

4. Setting up project Before we add any files to our project lets take a look at the project settings. So click on Project / Settings The following dialog appears:

Here you can change the: Project Name (self explanatory) Run Parameters (these are command line parameters that would be passed to the script which you were running) Working Directory (this is the directory in which the scripts would be executed, i.e. if you were to write a script that creates a file named output.txt and didnt specify in which folder to create it, then it would be created in the Working Directory. Best practice is to set this to your project folder. Remote Path is for use in remote debugging, which we will not cover in this tutorial. PYTHONPATH is another environment variable which can be defined in the system settings of your windows installation and/or here. It allows the python interpreter to locate modules which are included in the script youre running via the import and from python directives. It should be filled with a semi-colon separated list of folders which hold importable python modules. For now you can leave it blank. After you are done setting up the project, click Ok and lets move to the next step.

5. Adding files to your project. First lets click on File/New and a new blank file will be opened, now click File / Save As and name it something appropriate (i.e. first.py)
Once youve saved the file, you can right-click the project name icon on the project content display and select Add file to group, as follows:

Now you can select one or more files to add to the project (in our case just the one).

After this the file should appear underneath the project name icon:

6.

First run.

Before we run our first script lets add some content to it, so type in the following in the first.py window: print "Hello World" for i in range(10): print "this is the", i, "iteration of our first script" print "The end" note the tabbed space at the 3rd line! The content should now look like this:

Now to run the script do the following. First save the changes via File / Save and then click on Debug / Run Local Debug (or use F5 hotkey) A command prompt dialog will appear during the execution of the script and once the script is done it should disappear. However the output of the script will be shown in the bottom in the console output display and should look something like this:

7.

Step by step debugging and the variable watch.

Now to start debugging we need to add at least one break point ( this is the point where our execution of the script will pause until we allow it to continue further). To do this simply place the cursor on the line where you want to add this break point (best do it on line 3) and click Debugg / Breakpoints / Toggle breakpoint ( or use F9 hotkey). You should now see the red dot next to the line number. At this point we also might be interested in the value of the i variable at the point where breakpoint is inserted, so to add this variable to variable watch, do the following: First click View / Debug Windows / Locals/Globals (or the Alt +4 hotkey) and expand the newly displayed window enough that you can see its contents. Using your mouse select only the part of text where this variable is written (i.e.

) And then click on Debug / Add to Watch (or use Shift + F9 hotkey). You will notice that the variable doesnt immediately show in the Watch window, but dont worry it will be displayed during the next runs breakpoint! Now we are ready to do some debugging. Click Debug / Run Local Debug (or use F5 hotkey) and the script will start execution and pause when it reaches the break point, you can continue the execution by pressing: F5 for continued normal run (i.e. the script will continue to the next break point encounter or to the end of script, whichever comes first) F10 for Step Over, which will continue execution by one step without going into a called function, instead behaving as if the called function is a single step. F11 for Step Into, which will continue execution by one step, however it will consider stepping into a called function and if possible running just the first step of the called function. In our case F10 and F11 should behave exactly the same, while F5 will break at every iteration of the for loop. Note that the current position of the execution is always displayed using a small yellow triangle displayed next to the line numbers.

You will also be able to notice how the value of i changes with every iteration of the for loop in the Watch window

Here are a few pointers: 1. you can break the scripts execution at any time by clicking Debug / Stop debugging (Shift + F5) 2. you can add, remove and modify breakpoints during execution. 3. you can also add, remove and modify code during a breakpoint, however note that the newly added lines will disrupt the expected positions of the breakpoints and produce unreliable results, so its considered a bad practice. 4. you can modify the value of an variable using the Watch window during breakpoint! 5. you can place your mouse pointer over a variable during breakpoint to see its value in a tip window which will appear shortly after.

Vous aimerez peut-être aussi