Debugging
You can debug an IBM® Open Enterprise SDK for Python
application by using the built-in source code debugger via the pdb module.
pdb module is part of the Python standard library and can be used to set
conditional breakpoints, expression evaluation, view stack frames, and step through the code line by
line. The code below shows an example where a breakpoint is set inside a for
loop.import pdb
for i in range(10):
pdb.set_trace()
i_square = i * i
print("The square of {} is: {}".format(i, i_square))pdb_debugger.py and add the following lines:import os, sys
SCRIPT_DIR, SCRIPT_NAME = os.path.split(os.path.abspath(__file__))
PARENT_DIR = os.path.dirname(SCRIPT_DIR)
filename = __file__
def func_a():
x = 5
y = 15
z = x + y
return z
def func_b():
sum = 0
for i in range(5):
sum = sum + i
return sum
if __name__ == '__main__':
print("Running {}".format(SCRIPT_NAME))
z_ret = func_a()
sum_ret = func_b()
print("z_ret: {} \t\t sum_ret: {}".format(z_ret, sum_ret))To invoke the debugger,
run the script with the following command:python3 -m pdb pdb_debugger.pyYou
can see the debug prompt at the first line of the file and you can then step through the code by
using the commands outline in the pdb docs. For example, to break
func_b when it is called, execute the following command in the
prompt:break func_bThis command registers the breakpoint at that function
call. Start the file execution by inputting the command:cThis command now
starts executing the code line by line and stops the execution just before the first line of the
func_b function.python3 -m pdb -c "break func_b" pdb_debugger.pyMore information about the capabilities and documentation of the pdb module, see
pdb — The Python Debugger in the official Python documentation.
Python also includes the trace module, which can be used to monitor functions
and line execution. You can use the useful features that are provided by the trace
module such as code-coverage and you can run the module either from the command prompt or
incorporate the module into the program itself.
The example below gives a brief overview of the capabilities of using the trace
module.
trace module can create associated files that hold the trace results,
create a subfolder that houses the code to be run:$ mkdir -p fibonacciCreate the files with the following code:
*
fibonacci/fibonacci.py"""
In mathematics, the Fibonacci numbers, form a sequence, called the Fibonacci sequence, such that each number is the
sum of the two preceding ones, starting from F(0) = 0 and F(1) = 1, and for any integer n > 1: F(n) = F(n-1) + F(n-2).
[wikipedia](https://en.wikipedia.org/wiki/Fibonacci_number)
"""
def fib(x):
print("Processing fib({})".format(x))
if x<0:
print("Input needs to be a positive integer")
elif x==1:
return 0
elif x==2:
return 1
else:
return fib(x-1)+fib(x-2)
if __name__ == '__main__':
print(fib(15))*
fibonacci/main.pyfrom fibonacci import fib
def main():
print ("In the main program.")
fib(4)
return
if __name__ == '__main__':
main()*
fibonacci_trace.pyimport trace
from fibonacci.fibonacci import fib
tracer = trace.Trace(count=False, trace=True)
tracer.run('fib(4)')python -m trace --trace fibonacci/main.py--count option:python -m trace --count fibonacci/main.pypython -m trace --listfuncs fibonacci/main.pypython -m trace --listfuncs --trackcalls fibonacci/main.pypython fibonacci_trace.pyYou can learn more about the module and its full set of capabilities at trace — Trace or track Python statement execution in the
official Python documentation.