[Python](EN) Debug python code using pdb

Debug python code using pdb.


Environment and Prerequisite

  • Linux base system
  • Bash shell(/bin/bash)
  • Python


pdb

What is pdb?

  • pdb: Interactive source code debugger for Python programs.


How to run

pdb run method - 1

  • Run pdb in start time.
python3 -m pdb example.py
  • Run example
$ python -m pdb test.py
> /Users/taewoo/test.py(1)<module>()
-> def test_sum(a,b):
(Pdb)

pdb run method - 2

  • Add to code
# In python code
...
import pdb; pdb.set_trace()
...
  • Run example
$ python test.py
> /Users/taewoo/test.py(6)<module>()
-> b=30
(Pdb)


Usage

Usage command list

  • l: Print codes of around current point. Current point will be marked by arrow.
  • n: Move to next statement. In other words, run statement which is shown in above l command’s arrow and move to next statement.
  • s: Commonly used ‘Step Into’ command. --Call-- will be shown after stepped in and --Return-- will be shown after return.
  • c: Run codes until meet next break point.
  • r: Run command until meet return.
  • w: Show current call stack.
  • cl: Clear all break points.
  • b: Make break point to file’s or package’s specific function or line.
  • a: Print function’s all arguments.

Usage command example

  • l: Print codes of around current point. Current point will be marked by arrow.
(Pdb) l
  1  	def test_sum(a,b):
  2  	    return a+b
  3
  4  	a=10
  5  ->	b=30
  6  	c=test_sum(a,b)
  7
  8  	for i in range(1,10):
  9  	    a = a + i
 10
 11  	import requests
  • n: Move to next statement. In other words, run statement which is shown in above l command’s arrow and move to next statement.
(Pdb)  n
> /Users/taewoo/test.py(6)<module>()
-> c=test_sum(a,b)
  • s: Commonly used ‘Step Into’ command. --Call-- will be shown after stepped in and --Return-- will be shown after return.
(Pdb)  s
--Call--
> /Users/taewoo/test.py(1)test_sum()
-> def test_sum(a,b):
...
--Return--
> /Users/taewoo/test.py(5)test_sum()->40
-> return a+b
  • c: Run codes until meet next break point.
(Pdb) c
The program finished and will be restarted
  • r: Run command until meet return.
(Pdb) s
--Call--
> /Users/taewoo/test.py(1)test_sum()
-> def test_sum(a,b):
(Pdb) r
--Return--
> /Users/taewoo/test.py(5)test_sum()->40
-> return a+b
  • w: Show current call stack.
(Pdb) w
  /Users/taewoo/.pyenv/versions/3.6.2/lib/python3.6/bdb.py(431)run()
-> exec(cmd, globals, locals)
  <string>(1)<module>()
  /Users/taewoo/test.py(9)<module>()
-> c=test_sum(a,b)
> /Users/taewoo/test.py(5)test_sum()->40
-> return a+b
  • cl: Clear all break points.
(Pdb) cl
Clear all breaks? y
  • b: Make break point to file’s or package’s specific function or line.
(Pdb) b 8
Breakpoint 1 at /Users/taewoo/test.py:8
(Pdb) b test_sum
Breakpoint 2 at /Users/taewoo/test.py:1
# Stop at line number 8
(Pdb) c
> /Users/taewoo/test.py(8)<module>()
-> b=30
# Stop at function
(Pdb) c
> /Users/taewoo/test.py(2)test_sum()
-> tmp=1
(Pdb) b
Num Type         Disp Enb   Where
1   breakpoint   keep yes   at /Users/taewoo/test.py:8
	breakpoint already hit 2 times
2   breakpoint   keep yes   at /Users/taewoo/test.py:1
	breakpoint already hit 1 time
  • a: Print function’s all arguments.
(Pdb)  s
--Call--
> /Users/taewoo/test.py(1)test_sum()
-> def test_sum(a,b):
(Pdb) a
a = 10
b = 30

Others

  • We can check value of variable using print function or just type it to pdb.
(Pdb) c
> /Users/taewoo/test.py(2)test_sum()
-> tmp=1
(Pdb) n
> /Users/taewoo/test.py(3)test_sum()
-> tmp=2
(Pdb) tmp
1
(Pdb) print(tmp)
1
(Pdb) type(data)
<class 'dict'>
(Pdb) data
{'data': [{'type': 'articles', 'id': '1', 'attributes': {'title': 'JSON API paints my bikeshed!', 'body': 'The shortest article. Ever.', 'created': '2015-05-22T14:56:29.000Z', 'updated': '2015-05-22T14:56:28.000Z'}, 'relationships': {'author': {'data': {'id': '42', 'type': 'people'}}}}], 'included': [{'type': 'people', 'id': '42', 'attributes': {'name': 'John', 'age': 80, 'gender': 'male'}}]}
  • Add break point in code.
  • Pdb will be stopped on import pdb; pdb.set_trace()
def test_sum(a,b):
    tmp=1
    tmp=2
    tmp=3
    return a+b

a=10
b=30
c=test_sum(a,b)

for i in range(1,10):
    a = a + i

import requests
import pdb; pdb.set_trace()
url = 'https://gist.githubusercontent.com/TWpower/771f9dfc8d9e1ddc0ecbdaea5b2e379e/raw/2c7785b4835138255bdadb71bd83702e53ac2677/test-example.json'

data = requests.get(url).json()
$ python -m pdb test.py
> /Users/taewoo/test.py(1)<module>()
-> def test_sum(a,b):
(Pdb) c
> /Users/taewoo/test.py(16)<module>()
-> url = 'https://gist.githubusercontent.com/TWpower/771f9dfc8d9e1ddc0ecbdaea5b2e379e/raw/2c7785b4835138255bdadb71bd83702e53ac2677/test-example.json'
(Pdb)


Reference