SlideShare a Scribd company logo
5
Most read
9
Most read
Python Debugging Techniques

2013-10-21
Who am I
Tuomas Suutari
Software Developer at Anders Inno
8 years Python experience
Anders Inno
Software Company from Turku
specialized in e-commerce
www.andersinno.fi

2013-10-21
Outline
Basic techniques
Debuggers
Profiling

2013-10-21
Basic techniques
Print statements
Logging

2013-10-21
Print statements
●

●

●

The simplest way to see
what's happening
Sometimes also the fastest
and easiest, so don't
overthink it
Could cause problems
when temporary print
statements slip into
production

2013-10-21

def do_stuff():
something = do_something()
print 'Something:', something
do_more(something)
print 'Calling do_last'
do_last()
Logging
●

●

●

●

Python has powerful and
flexible logging module
Like print statements with
more context information
Used through a logger
object with name
Log messages have levels

2013-10-21

import logging
LOG = logging.getLogger(__name__)
def do_stuff():
something = do_something()
LOG.debug(
'Something: %s',
something)
do_more(something)
LOG.info('Calling do_last')
do_last()
Logging
●

Log output can be easily
controlled
•

•

●

Outputting to stdout, file,
network socket, ...
Filtering based on log levels
or logger names

Logging statements are
OK in production

2013-10-21

import logging
msg_format = (
'%(asctime)s %(name)s'
' %(levelname)s %(funcName)s'
' %(message)s')
logging.basicConfig(
format=msg_format,
level=logging.DEBUG)
Debuggers
PDB
Winpdb

2013-10-21
PDB
●

The Python Debugger

●

In standard library

●

Console interface

●

Launch script with PDB or
jump into debugger with
set_trace()

python -m pdb some_script.py

def do_stuff():
import pdb; pdb.set_trace()
something = do_something()
# ...

2013-10-21
PDB
●

Basic commands
•

c, cont: continue running

•

s, step: step into function

•

p, print: print value of variable/expression

•

b, break: set a breakpoint

•

u, up: move the current frame one level up

•

d, down: move the current frame one level down

•

l, list: list source code for the current file

2013-10-21
Winpdb
●

●

●

A Platform Independent
Python Debugger
Run Python script from the
GUI or embed to script
Attaching to embedded
debugger is possible from
a remote host too

2013-10-21

def do_stuff():
import rpdb2
rpdb2.start_embedded_debugger(
'passwd')
something = do_something()
# ...
Profiling
cProfile
Guppy

2013-10-21
cProfile
●

●

For profiling time
consumption of parts of the
program

python -m cProfile -s cumulative 

Visualizers for the data
could be useful

python -m cProfile -o out.pyprof 

•

Run Snake Run

•

KCacheGrind (with
pyprof2calltree)

2013-10-21

some_script.py

some_script.py
runsnake out.pyprof
Guppy
●

●

For profiling memory
consumption of parts of the
program

# Injecting Guppy memory dumping

Data structures in the
program that are growing
can be detected by
dumping memory profile
periodically with Guppy
and watching the process
with profile browser

hp.setref()

2013-10-21

# to the program:
from guppy import hpy
hp = hpy()
for x in a_loop_in_the_program:
hp.heap().dump('memprof.hpy')
# To start Profile Browser:
python -c "
from guppy import hpy;
hpy().pb()"
Guppy
●

Objects can be tracked
down by connecting a
monitor to running program

# Allow monitor to connect to the
# program
import guppy
from guppy.heapy import Remote
Remote.on()
# To connect with the monitor:
python -c "from guppy import hpy;
hpy().monitor()"
<Monitor> sc 1
<Annex> int
>>> hp.heap()

2013-10-21
Thank you!
Questions?

2013-10-21
Ad

Recommended

Python Programming Language
Python Programming Language
Laxman Puri
 
Beginning Python Programming
Beginning Python Programming
St. Petersburg College
 
Python Debugging Fundamentals
Python Debugging Fundamentals
cbcunc
 
Python introduction
Python introduction
Jignesh Kariya
 
Python final ppt
Python final ppt
Ripal Ranpara
 
Python ppt
Python ppt
Rachit Bhargava
 
Python - An Introduction
Python - An Introduction
Swarit Wadhe
 
Introduction to python programming
Introduction to python programming
Srinivas Narasegouda
 
Python basic
Python basic
Saifuddin Kaijar
 
Python basics
Python basics
RANAALIMAJEEDRAJPUT
 
Python Programming ppt
Python Programming ppt
ismailmrribi
 
introduction to Python (for beginners)
introduction to Python (for beginners)
guobichrng
 
Introduction to python for Beginners
Introduction to python for Beginners
Sujith Kumar
 
Packages In Python Tutorial
Packages In Python Tutorial
Simplilearn
 
PYTHON NOTES
PYTHON NOTES
Ni
 
File handling in Python
File handling in Python
Megha V
 
Evolutionary models
Evolutionary models
Pihu Goel
 
Python GUI Programming
Python GUI Programming
RTS Tech
 
Introduction to python
Introduction to python
eShikshak
 
Python Basics
Python Basics
tusharpanda88
 
User Defined Functions
User Defined Functions
Praveen M Jigajinni
 
Introduction to numpy
Introduction to numpy
Gaurav Aggarwal
 
Python course syllabus
Python course syllabus
Sugantha T
 
Data Structures in Python
Data Structures in Python
Devashish Kumar
 
Object oriented programming in python
Object oriented programming in python
baabtra.com - No. 1 supplier of quality freshers
 
Overview of python 2019
Overview of python 2019
Samir Mohanty
 
COCOMO Model in software project management
COCOMO Model in software project management
Syed Hassan Ali
 
Introduction to Python
Introduction to Python
Nowell Strite
 
PyCon AU 2012 - Debugging Live Python Web Applications
PyCon AU 2012 - Debugging Live Python Web Applications
Graham Dumpleton
 
Tips for Happier Python Debugging
Tips for Happier Python Debugging
Chun-Hao Chang
 

More Related Content

What's hot (20)

Python basic
Python basic
Saifuddin Kaijar
 
Python basics
Python basics
RANAALIMAJEEDRAJPUT
 
Python Programming ppt
Python Programming ppt
ismailmrribi
 
introduction to Python (for beginners)
introduction to Python (for beginners)
guobichrng
 
Introduction to python for Beginners
Introduction to python for Beginners
Sujith Kumar
 
Packages In Python Tutorial
Packages In Python Tutorial
Simplilearn
 
PYTHON NOTES
PYTHON NOTES
Ni
 
File handling in Python
File handling in Python
Megha V
 
Evolutionary models
Evolutionary models
Pihu Goel
 
Python GUI Programming
Python GUI Programming
RTS Tech
 
Introduction to python
Introduction to python
eShikshak
 
Python Basics
Python Basics
tusharpanda88
 
User Defined Functions
User Defined Functions
Praveen M Jigajinni
 
Introduction to numpy
Introduction to numpy
Gaurav Aggarwal
 
Python course syllabus
Python course syllabus
Sugantha T
 
Data Structures in Python
Data Structures in Python
Devashish Kumar
 
Object oriented programming in python
Object oriented programming in python
baabtra.com - No. 1 supplier of quality freshers
 
Overview of python 2019
Overview of python 2019
Samir Mohanty
 
COCOMO Model in software project management
COCOMO Model in software project management
Syed Hassan Ali
 
Introduction to Python
Introduction to Python
Nowell Strite
 
Python Programming ppt
Python Programming ppt
ismailmrribi
 
introduction to Python (for beginners)
introduction to Python (for beginners)
guobichrng
 
Introduction to python for Beginners
Introduction to python for Beginners
Sujith Kumar
 
Packages In Python Tutorial
Packages In Python Tutorial
Simplilearn
 
PYTHON NOTES
PYTHON NOTES
Ni
 
File handling in Python
File handling in Python
Megha V
 
Evolutionary models
Evolutionary models
Pihu Goel
 
Python GUI Programming
Python GUI Programming
RTS Tech
 
Introduction to python
Introduction to python
eShikshak
 
Python course syllabus
Python course syllabus
Sugantha T
 
Data Structures in Python
Data Structures in Python
Devashish Kumar
 
Overview of python 2019
Overview of python 2019
Samir Mohanty
 
COCOMO Model in software project management
COCOMO Model in software project management
Syed Hassan Ali
 
Introduction to Python
Introduction to Python
Nowell Strite
 

Similar to Python debugging techniques (20)

PyCon AU 2012 - Debugging Live Python Web Applications
PyCon AU 2012 - Debugging Live Python Web Applications
Graham Dumpleton
 
Tips for Happier Python Debugging
Tips for Happier Python Debugging
Chun-Hao Chang
 
Python debuggers slides
Python debuggers slides
mattboehm
 
Introduction to the Python Debugger (pdb)
Introduction to the Python Debugger (pdb)
Raul Cumplido
 
Python Programming Essentials - M28 - Debugging with pdb
Python Programming Essentials - M28 - Debugging with pdb
P3 InfoTech Solutions Pvt. Ltd.
 
Py tut-handout
Py tut-handout
Ramachandra Dama
 
summer training report on python
summer training report on python
Shubham Yadav
 
Debugging with PDB
Debugging with PDB
cdw9
 
Introduction to IPython & Jupyter Notebooks
Introduction to IPython & Jupyter Notebooks
Eueung Mulyana
 
Introduction to python
Introduction to python
Mohammed Rafi
 
Python - Lecture 3
Python - Lecture 3
Ravi Kiran Khareedi
 
Python Orientation
Python Orientation
Pavan Devarakonda
 
Migrating from matlab to python
Migrating from matlab to python
ActiveState
 
Python_book.pdf
Python_book.pdf
Dharmendra Jain
 
Python_Buildin_Data_types_Lecture_8.pptx
Python_Buildin_Data_types_Lecture_8.pptx
foxel54542
 
Python
Python
Praveen Konduru
 
Python
Python
prasad1ncc
 
Python
Python
JAVAID AHMAD WANI
 
Pythonpresent
Pythonpresent
Chui-Wen Chiu
 
Programming Under Linux In Python
Programming Under Linux In Python
Marwan Osman
 
PyCon AU 2012 - Debugging Live Python Web Applications
PyCon AU 2012 - Debugging Live Python Web Applications
Graham Dumpleton
 
Tips for Happier Python Debugging
Tips for Happier Python Debugging
Chun-Hao Chang
 
Python debuggers slides
Python debuggers slides
mattboehm
 
Introduction to the Python Debugger (pdb)
Introduction to the Python Debugger (pdb)
Raul Cumplido
 
Python Programming Essentials - M28 - Debugging with pdb
Python Programming Essentials - M28 - Debugging with pdb
P3 InfoTech Solutions Pvt. Ltd.
 
summer training report on python
summer training report on python
Shubham Yadav
 
Debugging with PDB
Debugging with PDB
cdw9
 
Introduction to IPython & Jupyter Notebooks
Introduction to IPython & Jupyter Notebooks
Eueung Mulyana
 
Introduction to python
Introduction to python
Mohammed Rafi
 
Migrating from matlab to python
Migrating from matlab to python
ActiveState
 
Python_Buildin_Data_types_Lecture_8.pptx
Python_Buildin_Data_types_Lecture_8.pptx
foxel54542
 
Programming Under Linux In Python
Programming Under Linux In Python
Marwan Osman
 
Ad

Recently uploaded (20)

PyCon SG 25 - Firecracker Made Easy with Python.pdf
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
 
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
Cyber Defense Matrix Workshop - RSA Conference
Cyber Defense Matrix Workshop - RSA Conference
Priyanka Aash
 
Python Conference Singapore - 19 Jun 2025
Python Conference Singapore - 19 Jun 2025
ninefyi
 
cnc-processing-centers-centateq-p-110-en.pdf
cnc-processing-centers-centateq-p-110-en.pdf
AmirStern2
 
The Future of Product Management in AI ERA.pdf
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
OpenPOWER Foundation & Open-Source Core Innovations
OpenPOWER Foundation & Open-Source Core Innovations
IBM
 
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
digitaljignect
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
You are not excused! How to avoid security blind spots on the way to production
You are not excused! How to avoid security blind spots on the way to production
Michele Leroux Bustamante
 
AI vs Human Writing: Can You Tell the Difference?
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
Lessons Learned from Developing Secure AI Workflows.pdf
Lessons Learned from Developing Secure AI Workflows.pdf
Priyanka Aash
 
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Josef Weingand
 
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Safe Software
 
"Database isolation: how we deal with hundreds of direct connections to the d...
"Database isolation: how we deal with hundreds of direct connections to the d...
Fwdays
 
Connecting Data and Intelligence: The Role of FME in Machine Learning
Connecting Data and Intelligence: The Role of FME in Machine Learning
Safe Software
 
Curietech AI in action - Accelerate MuleSoft development
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
Safe Software
 
PyCon SG 25 - Firecracker Made Easy with Python.pdf
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
 
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
Cyber Defense Matrix Workshop - RSA Conference
Cyber Defense Matrix Workshop - RSA Conference
Priyanka Aash
 
Python Conference Singapore - 19 Jun 2025
Python Conference Singapore - 19 Jun 2025
ninefyi
 
cnc-processing-centers-centateq-p-110-en.pdf
cnc-processing-centers-centateq-p-110-en.pdf
AmirStern2
 
The Future of Product Management in AI ERA.pdf
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
OpenPOWER Foundation & Open-Source Core Innovations
OpenPOWER Foundation & Open-Source Core Innovations
IBM
 
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
digitaljignect
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
You are not excused! How to avoid security blind spots on the way to production
You are not excused! How to avoid security blind spots on the way to production
Michele Leroux Bustamante
 
AI vs Human Writing: Can You Tell the Difference?
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
Lessons Learned from Developing Secure AI Workflows.pdf
Lessons Learned from Developing Secure AI Workflows.pdf
Priyanka Aash
 
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Josef Weingand
 
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Safe Software
 
"Database isolation: how we deal with hundreds of direct connections to the d...
"Database isolation: how we deal with hundreds of direct connections to the d...
Fwdays
 
Connecting Data and Intelligence: The Role of FME in Machine Learning
Connecting Data and Intelligence: The Role of FME in Machine Learning
Safe Software
 
Curietech AI in action - Accelerate MuleSoft development
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
Safe Software
 
Ad

Python debugging techniques

  • 2. Who am I Tuomas Suutari Software Developer at Anders Inno 8 years Python experience Anders Inno Software Company from Turku specialized in e-commerce www.andersinno.fi 2013-10-21
  • 5. Print statements ● ● ● The simplest way to see what's happening Sometimes also the fastest and easiest, so don't overthink it Could cause problems when temporary print statements slip into production 2013-10-21 def do_stuff(): something = do_something() print 'Something:', something do_more(something) print 'Calling do_last' do_last()
  • 6. Logging ● ● ● ● Python has powerful and flexible logging module Like print statements with more context information Used through a logger object with name Log messages have levels 2013-10-21 import logging LOG = logging.getLogger(__name__) def do_stuff(): something = do_something() LOG.debug( 'Something: %s', something) do_more(something) LOG.info('Calling do_last') do_last()
  • 7. Logging ● Log output can be easily controlled • • ● Outputting to stdout, file, network socket, ... Filtering based on log levels or logger names Logging statements are OK in production 2013-10-21 import logging msg_format = ( '%(asctime)s %(name)s' ' %(levelname)s %(funcName)s' ' %(message)s') logging.basicConfig( format=msg_format, level=logging.DEBUG)
  • 9. PDB ● The Python Debugger ● In standard library ● Console interface ● Launch script with PDB or jump into debugger with set_trace() python -m pdb some_script.py def do_stuff(): import pdb; pdb.set_trace() something = do_something() # ... 2013-10-21
  • 10. PDB ● Basic commands • c, cont: continue running • s, step: step into function • p, print: print value of variable/expression • b, break: set a breakpoint • u, up: move the current frame one level up • d, down: move the current frame one level down • l, list: list source code for the current file 2013-10-21
  • 11. Winpdb ● ● ● A Platform Independent Python Debugger Run Python script from the GUI or embed to script Attaching to embedded debugger is possible from a remote host too 2013-10-21 def do_stuff(): import rpdb2 rpdb2.start_embedded_debugger( 'passwd') something = do_something() # ...
  • 13. cProfile ● ● For profiling time consumption of parts of the program python -m cProfile -s cumulative Visualizers for the data could be useful python -m cProfile -o out.pyprof • Run Snake Run • KCacheGrind (with pyprof2calltree) 2013-10-21 some_script.py some_script.py runsnake out.pyprof
  • 14. Guppy ● ● For profiling memory consumption of parts of the program # Injecting Guppy memory dumping Data structures in the program that are growing can be detected by dumping memory profile periodically with Guppy and watching the process with profile browser hp.setref() 2013-10-21 # to the program: from guppy import hpy hp = hpy() for x in a_loop_in_the_program: hp.heap().dump('memprof.hpy') # To start Profile Browser: python -c " from guppy import hpy; hpy().pb()"
  • 15. Guppy ● Objects can be tracked down by connecting a monitor to running program # Allow monitor to connect to the # program import guppy from guppy.heapy import Remote Remote.on() # To connect with the monitor: python -c "from guppy import hpy; hpy().monitor()" <Monitor> sc 1 <Annex> int >>> hp.heap() 2013-10-21