Saturday 9 May 2015

How to debugg python programs with pdb



Debugging in Python

As a programmer, one of the first things that you need for serious program development is a debugger.

Python has a debugger, which is available as a module called pdb (for “Python DeBugger”, naturally!).


1. Let’s start with a simple program, epdb.py.
 

 
2. Insert the following statement at the beginning of your Python program. This statement imports the Python debugger module, pdb.

    import pdb

3. Now find a spot where you would like tracing to begin, and insert the following code:

    pdb.set_trace()

So now your program looks like this.


   
4. Now run your program from the command line as you usually do, which will probably look something like this:

    anusree@anusree-Inspiron-5547:~$ python epdb.py


Then it will show like this (pdb)

If you want to see where is the control of pointer then press lower case "l",




For execute the next line press lower case "n" and press "enter" key.




If you want to quit from that program then press lower case "q" and press "enter" key.







If you want to print the value of the variable then press lower case "p" or type "print" and press "enter" key.




If you want to simply turn off from the pdb prompt then press lower case "c" and "enter"  key.




When the program gets completely debugged the it will turn off from the pdb prompt.



No comments:

Post a Comment