Debugging with pdb in Python

Debugging Python code with pdb

You can use ʻimport pdb`, but here we will show you how to debug without changing the source code. Only frequently used commands will be covered.

How to get started

$ python -m pdb XXXXX.py

Or

$ python3 -m pdb XXXXX.py

Execution example

Example of program to execute

hello.py


msg = "Hello world"

def hello(txt):
	print(txt)

hello(msg)
print("Done.")

--pdb execution example

$ python3 -m pdb hello.py
> /path/to/current/directory/hello.py(1)<module>()
-> msg = "Hello world"
(Pdb)

How to use pdb

You can proceed with debugging by entering a command after (Pdb).

--Frequently used commands

command function
b(reak) [Number of lines or function name] Set a breakpoint on a row or function
c(ont(inue)) Run until the next breakpoint is reached
s(tep) Run current line(Stop at a function call)
n(ext) Run current line(If it is a function call, execute the function)
q(uit) Exit the debugger

You can omit the characters in parentheses in the command.

Execution example

$ python3 -m pdb hello.py
> /path/to/current/directory/hello.py(1)<module>()
-> msg = "Hello world"
(Pdb) b 6
Breakpoint 1 at /path/to/current/directory/hello.py:6
(Pdb) c
> /path/to/current/directory/hello.py(6)<module>()
-> hello(msg)
(Pdb) s
--Call--
> /path/to/current/directory/hello.py(3)hello()
-> def hello(txt):
(Pdb) s
> /path/to/current/directory/hello.py(4)hello()
-> print(txt)
(Pdb) n
Hello world
--Return--
> /path/to/current/directory/hello.py(4)hello()->None
-> print(txt)
(Pdb) c
Done.
The program finished and will be restarted
> /path/to/current/directory/hello.py(1)<module>()
-> msg = "Hello world"
(Pdb) q
$ 

reference

http://docs.python.jp/3/library/pdb.html

Recommended Posts

Debugging with pdb in Python
Working with LibreOffice in Python
Scraping with chromedriver in python
Working with sounds in Python
Scraping with Selenium in Python
Scraping with Tor in Python
Tweet with image in Python
Combined with permutations in Python
Testing with random numbers in Python
GOTO in Python with Sublime Text 3
Working with LibreOffice in Python: import
Scraping with Selenium in Python (Basic)
CSS parsing with cssutils in Python
Numer0n with items made in Python
Open UTF-8 with BOM in Python
Use rospy with virtualenv in Python3
Use Python in pyenv with NeoVim
Heatmap with Dendrogram in Python + matplotlib
Read files in parallel with Python
Password generation in texto with python
Use OpenCV with Python 3 in Window
Until dealing with python in Atom
Get started with Python in Blender
Working with DICOM images in Python
Write documentation in Sphinx with Python Livereload
Get additional data in LDAP with python
Quadtree in Python --2
Python in optimization
CURL in python
FizzBuzz with Python3
Metaprogramming in Python
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
python DS debugging
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Scraping with Python
Try logging in to qiita with Python
Python3> in keyword> True with partial match?
Exclusive control with lock file in Python
Statistics with python
Device monitoring with On-box Python in IOS-XE
Meta-analysis in Python
Unittest in python
Scraping with Python
Python with Go
Try working with binary data in Python
Python debugging tips
Draw Nozomi Sasaki in Excel with python
Tips for dealing with binaries in Python
Display Python 3 in the browser with MAMP
Page cache in Python + Flask with Flask-Caching
Twilio with Python
Epoch in Python
Discord in Python
Integrate with Python
Post Test 3 (Working with PosgreSQL in Python)
How to work with BigQuery in Python
Playing card class in Python (with comparison)
Sudoku in Python
DCI in Python