SlideShare ist ein Scribd-Unternehmen logo
1 von 16
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
NUMPY
NUMPY NUMPY
NUMPY
 
Oop concepts in python
Oop concepts in pythonOop concepts in python
Oop concepts in python
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
DJango
DJangoDJango
DJango
 
File Handling Python
File Handling PythonFile Handling Python
File Handling Python
 
Overview of python 2019
Overview of python 2019Overview of python 2019
Overview of python 2019
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
Introduction to Recursion (Python)
Introduction to Recursion (Python)Introduction to Recursion (Python)
Introduction to Recursion (Python)
 
Python
PythonPython
Python
 
Packages In Python Tutorial
Packages In Python TutorialPackages In Python Tutorial
Packages In Python Tutorial
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
Python Scipy Numpy
Python Scipy NumpyPython Scipy Numpy
Python Scipy Numpy
 
django
djangodjango
django
 
Python final ppt
Python final pptPython final ppt
Python final ppt
 
Introduction to pandas
Introduction to pandasIntroduction to pandas
Introduction to pandas
 
Date and Time Module in Python | Edureka
Date and Time Module in Python | EdurekaDate and Time Module in Python | Edureka
Date and Time Module in Python | Edureka
 
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Python Libraries and Modules
Python Libraries and ModulesPython Libraries and Modules
Python Libraries and Modules
 

Ähnlich wie Python debugging techniques

Mender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and GolangMender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and GolangMender.io
 
BBD Hands-on with Python. Practical Hands-on Workshop about "Behaviour Driven...
BBD Hands-on with Python. Practical Hands-on Workshop about "Behaviour Driven...BBD Hands-on with Python. Practical Hands-on Workshop about "Behaviour Driven...
BBD Hands-on with Python. Practical Hands-on Workshop about "Behaviour Driven...Hemmerling
 
DocDoku: Using web technologies in a desktop application. OW2con'15, November...
DocDoku: Using web technologies in a desktop application. OW2con'15, November...DocDoku: Using web technologies in a desktop application. OW2con'15, November...
DocDoku: Using web technologies in a desktop application. OW2con'15, November...OW2
 
DocDokuPLM presentation - OW2Con 2015 Community Award winner
DocDokuPLM presentation - OW2Con 2015 Community Award winnerDocDokuPLM presentation - OW2Con 2015 Community Award winner
DocDokuPLM presentation - OW2Con 2015 Community Award winnerDocDoku
 
ContainerDayVietnam2016: Django Development with Docker
ContainerDayVietnam2016: Django Development with DockerContainerDayVietnam2016: Django Development with Docker
ContainerDayVietnam2016: Django Development with DockerDocker-Hanoi
 
PL-4047, Big Data Workload Analysis Using SWAT and Ipython Notebooks, by Moni...
PL-4047, Big Data Workload Analysis Using SWAT and Ipython Notebooks, by Moni...PL-4047, Big Data Workload Analysis Using SWAT and Ipython Notebooks, by Moni...
PL-4047, Big Data Workload Analysis Using SWAT and Ipython Notebooks, by Moni...AMD Developer Central
 
PyConUK 2014 - PostMortem Debugging and Web Development Updated
PyConUK 2014 - PostMortem Debugging and Web Development UpdatedPyConUK 2014 - PostMortem Debugging and Web Development Updated
PyConUK 2014 - PostMortem Debugging and Web Development UpdatedAlessandro Molina
 
Getting Started with Performance Co-Pilot
Getting Started with Performance Co-PilotGetting Started with Performance Co-Pilot
Getting Started with Performance Co-PilotPaul V. Novarese
 
Pentester++
Pentester++Pentester++
Pentester++CTruncer
 
Linux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudLinux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudAndrea Righi
 
Extending Android's Platform Toolsuite
Extending Android's Platform ToolsuiteExtending Android's Platform Toolsuite
Extending Android's Platform ToolsuiteOpersys inc.
 
BKK16-302: Android Optimizing Compiler: New Member Assimilation Guide
BKK16-302: Android Optimizing Compiler: New Member Assimilation GuideBKK16-302: Android Optimizing Compiler: New Member Assimilation Guide
BKK16-302: Android Optimizing Compiler: New Member Assimilation GuideLinaro
 
OSMC 2014 | Naemon 1, 2, 3, N by Andreas Ericsson
OSMC 2014 | Naemon 1, 2, 3, N by Andreas EricssonOSMC 2014 | Naemon 1, 2, 3, N by Andreas Ericsson
OSMC 2014 | Naemon 1, 2, 3, N by Andreas EricssonNETWAYS
 
Grunt training deck
Grunt training deckGrunt training deck
Grunt training deckJames Ford
 
Introducing Yeoman 1.0 beta
Introducing Yeoman 1.0 betaIntroducing Yeoman 1.0 beta
Introducing Yeoman 1.0 betadigitalzombie
 
Run Go applications on Pico using TinyGo
Run Go applications on Pico using TinyGo Run Go applications on Pico using TinyGo
Run Go applications on Pico using TinyGo Yu-Shuan Hsieh
 

Ähnlich wie Python debugging techniques (20)

Drools & jBPM Workshop Barcelona 2013
Drools & jBPM Workshop  Barcelona 2013Drools & jBPM Workshop  Barcelona 2013
Drools & jBPM Workshop Barcelona 2013
 
Mender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and GolangMender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and Golang
 
BBD Hands-on with Python. Practical Hands-on Workshop about "Behaviour Driven...
BBD Hands-on with Python. Practical Hands-on Workshop about "Behaviour Driven...BBD Hands-on with Python. Practical Hands-on Workshop about "Behaviour Driven...
BBD Hands-on with Python. Practical Hands-on Workshop about "Behaviour Driven...
 
DocDoku: Using web technologies in a desktop application. OW2con'15, November...
DocDoku: Using web technologies in a desktop application. OW2con'15, November...DocDoku: Using web technologies in a desktop application. OW2con'15, November...
DocDoku: Using web technologies in a desktop application. OW2con'15, November...
 
DocDokuPLM presentation - OW2Con 2015 Community Award winner
DocDokuPLM presentation - OW2Con 2015 Community Award winnerDocDokuPLM presentation - OW2Con 2015 Community Award winner
DocDokuPLM presentation - OW2Con 2015 Community Award winner
 
ContainerDayVietnam2016: Django Development with Docker
ContainerDayVietnam2016: Django Development with DockerContainerDayVietnam2016: Django Development with Docker
ContainerDayVietnam2016: Django Development with Docker
 
PL-4047, Big Data Workload Analysis Using SWAT and Ipython Notebooks, by Moni...
PL-4047, Big Data Workload Analysis Using SWAT and Ipython Notebooks, by Moni...PL-4047, Big Data Workload Analysis Using SWAT and Ipython Notebooks, by Moni...
PL-4047, Big Data Workload Analysis Using SWAT and Ipython Notebooks, by Moni...
 
PyConUK 2014 - PostMortem Debugging and Web Development Updated
PyConUK 2014 - PostMortem Debugging and Web Development UpdatedPyConUK 2014 - PostMortem Debugging and Web Development Updated
PyConUK 2014 - PostMortem Debugging and Web Development Updated
 
Getting Started with Performance Co-Pilot
Getting Started with Performance Co-PilotGetting Started with Performance Co-Pilot
Getting Started with Performance Co-Pilot
 
Pentester++
Pentester++Pentester++
Pentester++
 
Multiprocessing in python
Multiprocessing in pythonMultiprocessing in python
Multiprocessing in python
 
Linux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudLinux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloud
 
Extending Android's Platform Toolsuite
Extending Android's Platform ToolsuiteExtending Android's Platform Toolsuite
Extending Android's Platform Toolsuite
 
BKK16-302: Android Optimizing Compiler: New Member Assimilation Guide
BKK16-302: Android Optimizing Compiler: New Member Assimilation GuideBKK16-302: Android Optimizing Compiler: New Member Assimilation Guide
BKK16-302: Android Optimizing Compiler: New Member Assimilation Guide
 
OSMC 2014 | Naemon 1, 2, 3, N by Andreas Ericsson
OSMC 2014 | Naemon 1, 2, 3, N by Andreas EricssonOSMC 2014 | Naemon 1, 2, 3, N by Andreas Ericsson
OSMC 2014 | Naemon 1, 2, 3, N by Andreas Ericsson
 
Grunt training deck
Grunt training deckGrunt training deck
Grunt training deck
 
Introducing Yeoman 1.0 beta
Introducing Yeoman 1.0 betaIntroducing Yeoman 1.0 beta
Introducing Yeoman 1.0 beta
 
Python on pi
Python on piPython on pi
Python on pi
 
Run Go applications on Pico using TinyGo
Run Go applications on Pico using TinyGo Run Go applications on Pico using TinyGo
Run Go applications on Pico using TinyGo
 
Drools & jBPM Workshop London 2013
Drools & jBPM Workshop London 2013Drools & jBPM Workshop London 2013
Drools & jBPM Workshop London 2013
 

Kürzlich hochgeladen

Empowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintEmpowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintMahmoud Rabie
 
Machine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfMachine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfAijun Zhang
 
Bird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemBird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemAsko Soukka
 
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfDaniel Santiago Silva Capera
 
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Will Schroeder
 
Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.YounusS2
 
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfDianaGray10
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024D Cloud Solutions
 
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostKubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostMatt Ray
 
Comparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and IstioComparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and IstioChristian Posta
 
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...Aggregage
 
UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7DianaGray10
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Adtran
 
How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?IES VE
 
20230202 - Introduction to tis-py
20230202 - Introduction to tis-py20230202 - Introduction to tis-py
20230202 - Introduction to tis-pyJamie (Taka) Wang
 
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfJamie (Taka) Wang
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1DianaGray10
 
NIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopNIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopBachir Benyammi
 

Kürzlich hochgeladen (20)

Empowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintEmpowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership Blueprint
 
Machine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfMachine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdf
 
Bird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemBird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystem
 
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
 
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
 
Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.
 
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
 
201610817 - edge part1
201610817 - edge part1201610817 - edge part1
201610817 - edge part1
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024
 
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostKubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
 
Comparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and IstioComparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and Istio
 
20150722 - AGV
20150722 - AGV20150722 - AGV
20150722 - AGV
 
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
 
UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™
 
How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?
 
20230202 - Introduction to tis-py
20230202 - Introduction to tis-py20230202 - Introduction to tis-py
20230202 - Introduction to tis-py
 
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
 
NIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopNIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 Workshop
 

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