The Netbeans Python Debugger
As mentioned previously, the Netbeans IDE also includes a Python debugger that is derived from JeanYves Mengant's jpydbg debugger. This section will discuss how to make use of the Jython Guide" href="/jython-guide/other-netbeans-python-features.html">Netbeans Python debugger along with some examples using our HockeyRoster code that was written in the previous section. If you have used a debugger in another IDE, or perhaps the Java debugger that is available for Netbeans, this debugger will feel quite familiar. The Python debugger includes many features such as breakpoints, run-time local variable values, code stepping, and more.
Prior to using the debugger, it may be useful to take a look at the debugger preferences by navigating to the Netbeans Preferences>Python Options>Debugger window. From there you will see that you have the ability to change the debugger port, code coloring for debugging sessions, and to stop at the first line of the script or continue until the debugger reaches the first breakpoint. To make the debugger feel and act similar to the Netbeans Java debugger, you may want to de-select the "Stop at the first line" checkbox. Otherwise the debugger will not load your module right away, but rather stop execution at the first line of your module and wait for you to continue. See Figure 11-10.
- Figure 11-10. The Netbeans Python debugger
Making use of the Python debugger included with Netbeans is much like working from the Jython interactive interpreter from the command-line or terminal window. If you have selected the "Stop at first line" checkbox in the debugger preferences, the debugger will halt at the first line of code in your main module and you must use the debugger Continue button to move to the first line of code that is executed. However, if you have de-selected the checkbox, then the module will automatically run your program until it reaches the first breakpoint. For the purposes of this exercise, let's keep the checkbox selected. In order to set a breakpoint, click on the margin to the left of the line in your code where you would like the debugger to halt program execution. In our case, let's open the HockeyRoster.py module and set a breakpoint in the code as shown in Figure 11-11.
- Figure 11-11. Setting a breakpoint in the code
Now that we've set a breakpoint, we need to start our debugger. However, prior to debugging it is important to make sure that Netbeans knows which module to use for starting the program. To do so, right-click on your project and select Properties. When the properties window opens, select Run in the left-hand side of the window. You should now type or browse to the module that you wish to use as a starting point for your program. See Figure 11-12.
- Figure 11-12. Click Browse to select the module you wish to use as a starting point.
Note that this may already be automatically filled in for you by Netbeans. Once you've ensured that you have set the main module, you can begin the debugging session. To do so, you can either select your program and use the Debug menu option, or you can right-click on the project and select Debug. Once you've started the debugger, you will see a series of messages appearing in the debugging window near the bottom of the IDE window to indicate that the debugger has been started. After a few seconds, you will see the messages stop writing in the debugger output window, and the editor will focus on the first line of code in your main module and highlight it in green. See Figure 11-13.
- Figure 11-13. Beginning the debugging session
To continue the debugger to the first line of code that is executed, select the green Continue button in the toolbar, or press the F5 key. You should see the program will begin to execute within the debugger output window and it will halt to allow us to enter a selection. See Figure 11-14.
Post a comment