python debugging


How to debug python scripts? Debugging is one of the most important things you have to do when you write a code and sometimes you have to debug to find an issue or debug code written by another developer to find behavior and business logic behind the code.

How to debug python code?

Python has a built-in module for debugging ‘pdb’.

The easiest way to get in debug command-line mode is as follows (x.py is the python script you are trying to debug):

bitarray $ python -m pdb x.py 
> /tmp/x.py(2)<module>()
-> import re
(Pdb) 

Type ? on the debug prompt and you will see all available help topics.

(Pdb) ?

Documented commands (type help <topic>):
========================================
EOF    bt         cont      enable  jump  pp       run      unt   
a      c          continue  exit    l     q        s        until 
alias  cl         d         h       list  quit     step     up    
args   clear      debug     help    n     r        tbreak   w     
b      commands   disable   ignore  next  restart  u        whatis
break  condition  down      j       p     return   unalias  where 

Miscellaneous help topics:
==========================
exec  pdb

Undocumented commands:
======================
retval  rv

 

To get details about commands type help <command>

(Pdb) help where
w(here)
Print a stack trace, with the most recent frame at the bottom.
An arrow indicates the "current frame", which determines the
context of most commands.  'bt' is an alias for this command.
(Pdb) help  b
b(reak) ([file:]lineno | function) [, condition]
With a line number argument, set a break there in the current
file.  With a function name, set a break at first executable line
of that function.  Without argument, list all breaks.  If a second
argument is present, it is a string specifying an expression
which must evaluate to true before the breakpoint is honored.

The line number may be prefixed with a filename and a colon,
to specify a breakpoint in another file (probably one that
hasn't been loaded yet).  The file is searched for on sys.path;
the .py suffix may be omitted.
(Pdb) 

 

Here is a very simple python file (j.py) which we can debug:

#!/usr/bin/python

x=1

def y(z):
 print (z)
 d=[1,2,3,4];
 for i in d:
  print (i)

y(5)
 
bitarray# 

Run the below command to get in debug mode:

bitarray# python -m pdb j.py
> /home/abhishekjdl/j.py(3)<module>()
-> x=1
(Pdb) 
############# type 'l' to find lines around the current line (see the marker which points to line 3)
(Pdb) l
  1  	#!/usr/bin/python
  2  	
  3  ->	x=1
  4  	
  5  	def y(z):
  6  	 print (z)
  7  	 d=[1,2,3,4];
  8  	 for i in d:
  9  	  print (i)
 10  	
 11  	y(5)
(Pdb) 


############## type 'n' to go to the next line which is function declaration
(Pdb) n
>j.py(5)()
-> def y(z):
(Pdb) 


############# it will not go inside the function as this is just declaration and not calling the function.
############# variable 'x' has been defined at this point so we can print value of 'x'
############# type print (x) to print value of x
(Pdb) print (x)
1
(Pdb) 


############ type n again to go to the next line which has 'y (5)'
(Pdb) n
> j.py(11)()
-> y(5)


############ here we can step inside the function if you need to debug 
############ type 's' to step in

(Pdb) s
--Call--
>j.py(5)y()
-> def y(z):
(Pdb) 

############ Now you can continue debugging the code as usual.
############ you can type 'return' to return to go inside the loop to debug a particular iteration


#### to find type of variable you can use whatis
(Pdb) whatis x
<type 'int'>
(Pdb) 

 

setting up breakpoint

Your code might be few hundred lines which include multiple modules and sometimes you would like to run your code just before a certain line. You can use ‘breakpoint’ to run your code up to that line! You can setup multiple breakpoints

bitarray# python -m pdb j.py
> j.py(3)<module>()
-> x=1
(Pdb) b 7                  #### setting breakpoint on line 7
Breakpoint 1 at j.py:7   
(Pdb) c                    #### telling debugger to continue (run until we hit breakpoint)
5
> j.py(7)y()
-> d=[1,2,3,4];            #### so we are now at line 7 and we can resume debugging from here. 

(Pdb) 

##########################  see breakpoint line marked with 'B'

(Pdb) l
  3  	x=1
  4  	
  5  	def y(z):
  6  	 print (z)
  7 B	 d=[1,2,3,4];
  8  ->	 for i in d:
  9  	  print (i)
 10  	
 11  	y(5)
 12  	 
[EOF]

 

How to debug python CGI scripts?

So CGI scripts are called through a browser and if you want to see exceptions (Instead of “Internal Server Error”) you can add the following lines in your python code.

import cgitb
cgitb.enable()

By enabling cgitb any uncaught exception with details will be printed on the browser. It’s very useful for debugging Python scripts.

+ There are no comments

Add yours