Debugging

You can debug an IBM® Open Enterprise SDK for Python application by using the built-in source code debugger via the pdb module.

The 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))
You don't necessarily need to import pdb in an application to debug. You can invoke the debugger in a script by calling it from the command line. Create a new file called 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.py
You 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_b
This command registers the breakpoint at that function call. Start the file execution by inputting the command:
c
This command now starts executing the code line by line and stops the execution just before the first line of the func_b function.
Alternatively, you can achieve the same by passing additional parameters when invoking the debugger to run the script:
python3 -m pdb -c "break func_b" pdb_debugger.py

More 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.

Since the trace module can create associated files that hold the trace results, create a subfolder that houses the code to be run:
$ mkdir -p fibonacci

Create 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.py
from fibonacci import fib
def main():
    print ("In the main program.")
    fib(4)
    return
if __name__ == '__main__':
    main()
* fibonacci_trace.py
import trace
from fibonacci.fibonacci import fib
tracer = trace.Trace(count=False, trace=True)
tracer.run('fib(4)')
To see which statements are being executed as the program runs, run the command:
python -m trace --trace fibonacci/main.py
To run the code coverage to see which lines are run and which are skipped, use the --count option:
python -m trace --count fibonacci/main.py
To see relationships between function calls, run the command:
python -m trace --listfuncs fibonacci/main.py
For more details, run the command:
python -m trace --listfuncs --trackcalls fibonacci/main.py
To invoke the trace from a python script, run the command:
python fibonacci_trace.py

You 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.