Sunday, 3 June 2018

Python Debugger

Debugging is the process of looking for and resolving the issues in the code, that prevent the software from running correctly.

The Python debugger provides a debugging environment for Python programs. It supports setting conditional breakpoints, stepping through the source code on line at a time, stack inspection and show code as it executes.

At the top of your code import pdb module:
import pdb   

Now whereever you want to check the code insert break point :
pdb.set_trace() 

Following are the commands used by debugger at command line :

n                execute next line
c                 complete execution
l                  list 3 lines before and after execution
s                 step into function call
b                 show list of all break points
b[int]          set break point at line number
b [func]      break at function name
cl                clear all break points
cl[int]         clear all break points at line number
p                 print


For example  :

import pdb

def say_hello():
    pdb.set_trace()
    return "Hello pals"

print(say_hello())

A we run this program , we will run into a debugger :

> c:\python27\say_hello.py(5)say_hello()
-> return "Hello pals"
(Pdb) 


Now from the above commands we can run the debugger and locate the bug, if any.

No comments:

Post a Comment

Loan Prediction Analysis

In this post I will share my visuals ,learning and understanding of a dataset and using various algorithms of Machine Learning to analyse i...