Python Debugger

Python Programming Debugger

Debugger is one of the important thing any programming language to debug the code flow and used to identify the issues.

Debugger also saves you time and improves your programming skills.

In python programming, standard library has python debugger tool pdb which is used to debug the python code.

pdb has the most features needed for debugging such as breakpoints, single line stepping, inspection of stack frames, and so on.

Learning basic knowledge of using pdb will be useful in the environments where you can’t install other enhanced debugger.

How to run python debugger pdb for your python program ?

This python program is used read list of elements and prints in custom format using for loop and enumerate function.

!/usr/bin/python                                                          	 
                                                                           	 
list_arr = [10, 20, 30, 40, 50]                                            	 
                                                                           	 
for index, val in enumerate(list_arr,1):                                   	 
	print("index: %d -> val: %d" % (index, val))  

Two different ways to use python debugger pdb in your program, there are using in command line arguments or inside python code.

Run Python Debugger from Command Line

This is one of the way to run python debugger for any python program but here once program execution is done, manually needs to exit from python debugger ‘pdb’..

In python2.7,

$ python -m pdb for_enumerate.py
> /home/tmp/python/for_enumerate.py(3)<module>()
-> list_arr = [10, 20, 30, 40, 50]
(Pdb)

In python3,

$ python3 -m pdb for_enumerate.py
> /home/tmp/python/for_enumerate.py(3)<module>()
-> list_arr = [10, 20, 30, 40, 50]
(Pdb)

Python Programming Debugger Breakpoint

In python programming, we can also the set the breakpoint to debug the code by importing pdb module.

#!/usr/bin/python                                                          	 
                                                                           	 
list_arr = [10, 20, 30, 40, 50]                                            	 
                                                                           	 
for index, val in enumerate(list_arr,1):                                   	 
	import pdb; pdb.set_trace()                                            	 
	print("index: %d -> val: %d" % (index, val))                           	                                      

Navigating Python Debugger

Most of the programming, important in debugging is to navigate the execution stack.

Once the Python debugger is running with python debugger, below commands are useful to debug the code.

w: Shows which line is currently executed and where is the execution stack in python program.

l : List the more context or code around the current the location in python program.

u: Navigates the call stack up in python program.

d: Navigates the call stack down in python program.

Stepping Python Debugger

n: Moving debugger execution to the next line

s: Executes the current line and stop to debug in the location of the function which is called.

c: Continues execution and stops to debug at the next breakpoint in python program.

Python Debugger Example

$ python3 -m pdb for_enumerate.py
> /home/tmp/python/for_enumerate.py(3)<module>()
-> list_arr = [10, 20, 30, 40, 50]
(Pdb) n
> /home/tmp/python/for_enumerate.py(5)<module>()
-> for index, val in enumerate(list_arr,1):
(Pdb) n
> /home/tmp/python/for_enumerate.py(6)<module>()
-> print("index: %d -> val: %d" % (index, val))
(Pdb) n
index: 1 -> val: 10
> /home/tmp/python/for_enumerate.py(5)<module>()
-> for index, val in enumerate(list_arr,1):
(Pdb) n
> /home/tmp/python/for_enumerate.py(6)<module>()
-> print("index: %d -> val: %d" % (index, val))
(Pdb) n
index: 2 -> val: 20
> /home/tmp/python/for_enumerate.py(5)<module>()
-> for index, val in enumerate(list_arr,1):
(Pdb) w
  /usr/lib64/python3.5/bdb.py(431)run()
-> exec(cmd, globals, locals)
  <string>(1)<module>()
> /home/tmp/python/for_enumerate.py(5)<module>()
-> for index, val in enumerate(list_arr,1):
(Pdb) l
  1 	 #!/usr/bin/python
  2 	 
  3 	 list_arr = [10, 20, 30, 40, 50]
  4 	 
  5  ->    for index, val in enumerate(list_arr,1):
  6 	 	print("index: %d -> val: %d" % (index, val))
[EOF]
(Pdb) u
> <string>(1)<module>()
(Pdb) d
> /home/tmp/python/for_enumerate.py(5)<module>()
-> for index, val in enumerate(list_arr,1):
(Pdb) s
> /home/tmp/python/for_enumerate.py(6)<module>()
-> print("index: %d -> val: %d" % (index, val))
(Pdb) c
index: 3 -> val: 30
index: 4 -> val: 40
index: 5 -> val: 50
The program finished and will be restarted
> /home/tmp/python/for_enumerate.py(3)<module>()
-> list_arr = [10, 20, 30, 40, 50]
(Pdb)

How to print variable while debugging ?

Either we can use variable name or using p and variable name to print the variable value in any debugging breakpoint.

$ python for_enumerate.py
> /home/tmp//python/for_enumerate.py(7)<module>()
-> print("index: %d -> val: %d" % (index, val))
(Pdb) index
1
(Pdb) val
10
(Pdb) p index
1
(Pdb) p 10
10

How to exit the python debugger ?

exit is used to exit python debugger.

Enhanced Python Debugger

In python programming, enhanced debuggers are also available to provide a better user experience.

Mostly adds useful extra features to pdb python debugger like below

  • syntax highlighting
  • better tracebacks
  • introspection

Some Popular enhanced python debuggers are IPython’s ipdb and pdb++.

PythonEnhanced Debugger ipdb

To install the IPython ipdb, use pip in the virtual environment.

$ virtualenv venv

Once installed, activate virtual environment.

$ . venv/bin/activate

Install idpb python debugger using pip command.

$ pip install idpb

Python Enchanced Debugger idpb Example

Set the debugger breakpoint in the python program and run the python program, ipdb debugger works.

import ipdb; ipdb.set_trace()

otherwise run ipdb debugger in command line as below.

$ python -m ipdb for_enumerate.py
> /home/tmp/python/for_enumerate.py(3)<module>()
  	2
----> 3 list_arr = [10, 20, 30, 40, 50]
  	4

ipdb> n
> /home/tmp/python/for_enumerate.py(5)<module>()
  	4
----> 5 for index, val in enumerate(list_arr,1):
  	6 	import pdb; pdb.set_trace()

ipdb> n
> /home/tmp/python/for_enumerate.py(6)<module>()
  	5 for index, val in enumerate(list_arr,1):
----> 6 	import pdb; pdb.set_trace()
  	7 	print("index: %d -> val: %d" % (index, val))

ipdb> n
> /home/tmp/python/for_enumerate.py(7)<module>()
-> print("index: %d -> val: %d" % (index, val))
(Pdb) n
index: 1 -> val: 10
> /home/tmp/python/for_enumerate.py(5)<module>()
-> for index, val in enumerate(list_arr,1):
(Pdb) c
> /home/tmp/python/for_enumerate.py(7)<module>()
-> print("index: %d -> val: %d" % (index, val))
(Pdb) c
index: 2 -> val: 20
> /home/tmp/python/for_enumerate.py(6)<module>()
-> import pdb; pdb.set_trace()
(Pdb) c
index: 3 -> val: 30
> /home/tmp/python/for_enumerate.py(7)<module>()
-> print("index: %d -> val: %d" % (index, val))
(Pdb) c
index: 4 -> val: 40
> /home/tmp/python/for_enumerate.py(6)<module>()
-> import pdb; pdb.set_trace()
(Pdb) c
index: 5 -> val: 50
The program finished and will be restarted
> /home/tmp/python/for_enumerate.py(3)<module>()
  	2
----> 3 list_arr = [10, 20, 30, 40, 50]
  	4

Python Enhanced Debugger pdb++

This is also similar like ipdb and also can be used in virtual environment.

Enable virtual environment.

$ . venv/bin/activate

Install pdb++ debugger pip install pdbp

Set the debugger breakpoint in the python program and run the python program, pdb++ debugger works.

import pdb; pdb.set_trace()


Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^