This slide targets the people who want to improve their Python debugging skills thus have a happier life.
Especially, this is for those who have strong typing language experiences(C, C++, and Java) but don't know how to Python properly.
2. To Audience
This slide targets the people who want to improve their Python debugging
skills thus have a happier life.
Especially, this is for those who have strong typing language experiences(C,
C++, and Java) but don't know to to Python properly.
4. Overview
● Duck Typing: The loved and hated
● Interactive Programming: That's the Pythonic Way
● IPython: Powerful Interactive Shell
● Logging and Print: Basic but Useful
● IPDB: Python Debugger
● Debugging Tips: Unit Test, Try-Except Clause, Pickle and Dill
5. Duck Typing
If it walks like a duck and it quacks like a duck, then it must be a duck
A humorous and apt
representation of duck typing.
Source: Mastracci, 2014.
7. Duck Typing: the exceptional Enum
The enum object is comparing via their identity instead of value.
8. Duck Typing: Source of Surprises
Python is not a strong type language. It favors flexibility over safety.
This cause unexpected errors and troubles, especially for those who write
Python like C++ ʕ ͡° ͜ʖ ͡°ʔ .
The following slides will introduce interactive programming to avoid it and
debugging tools.
10. Interactive Programming: What you can do
In a REPL environment, you can do the following:
● Manipulate existing variables
● Call a function
● Define function and class
● Load a Python script
● Overwrite existing variables, function and class
● Debugging
● Read the documents and source codes of an imported function and class
● $ sudo rm -rf /*
● Basically, whatever you want ( ͡◉◞ ͜ʖ◟ ͡◉)
11. Interactive Programming: REPL-DD
You should keep testing and refining in REPL during development.
Don’t start testing your code until you finish everything!
12. Interactive Programming: Tools
There are awesome REPL tools like Jupyter Notebook, IPython, and the latest
Jupyter-Lab.
This slide does not cover Jupyter, but it’s a powerful for you to code, visualize,
and share the result in browsers. Please do search for it if you didn’t know it.
14. IPython: Basic Usage
#you can also specify the version by ipython2 or ipython3
$ipython
#loading a python scripts
[1] run my_example.py
#read the documents of the imported function
[2] example_fun?
#call a function from the previous script
[3] example_fun(3)
#leaving the shell
[4] exit
15. IPython: Magic Commands
Commands begin with % are magic commands making your life better . These
commands are not Python code. Only use them within IPython or Jupyter
● %time and %%time #measure times of a command
● %timeit and %%timeit #measure times of multiple lines commands
● %store #save a variable for another shell.
● %pdb #launch debugger when an exception occurred.
● %debug #post-mortem debugging
16. IPython: embed()
There are cases you can’t launch python scripts with IPython easily. For
example, ROS launch file and Python-written CMD.
● You can use IPython.embed() as a breakpoint to spawn an IPython
shell.
● The spawned shell is a copy of the original process, any modification is
irrelevant to the original process
17. IPython: Global Flag with embed()
If you insert IPython.embed() within a loop or in a function which might be
called multiple times. Make sure you use a global variable to prevent
IPython.embed() is called multiple times
18. Logging and Print
In debugging, print is easy and convenient. But it’s messy and tedious when
you print debug message everywhere in your codes.
logging module provides you an elegant way to manage your printing.
● Level of messages: DEBUG, INFO, WARNING, ERROR, and CRITICAL
● Format your printing easily and uniformly
● Send messages to different places easily.
During debugging, you can use logging.debug to print the detailed messages,
and set the logging level to debug.
After debug, you can raise the logging level to omit those messages.
19. Logging and Print: Example of Logging
From official Python documentation site
20. Logging and Print: Misleading Messages
Be careful, what you print might be misleading!
21. IPDB: Interactive Python Debugger
Except for the Debugger in PyCharm, you can also use IPDB(interactive PDB)
to debug.
Compare to IPython, IPDB enables line by line, iteration by iteration, step
into a function call, stop at a breakpoint.
22. IPDB: Basic Command
● n #next line, a function call or a loop is considered as one line
● s #step into a function or a loop
● c #continue until exception or breakpoint
● w #shows where you are in the codes.
● r #continue until the current function is returned or a loop is over.
● b 3 #breakpoint at line-3
● j 10 #jump to line-10
23. IPDB: Launch by set_trace()
Besides of launch IPDB from terminal, you can insert ipdb.set_trace() inside
your code, similar to IPython.embed(). This manual fashion is handy when you
can’t launch Python script directly.
24. IPDB: Global Flag with set_trace()
Just like IPython.embed(), you can use a global flag to prevent
ipdb.set_trace() is called multiple times.
25. IPDB: Launch by pm()
IPDB also provide post mortem debugging when an exception occurred.
You can use ipdb.pm() in a try-except clause or a REPL environment.
Launch ipdb.pm() before an exception causes error because of missing
sys.last_traceback
In IPython, the magic command %debug is equivalent to ipdb.pm()
26. IPDB: Automatic Launch by %pdb
In IPython, the %pdb command allows you to launch IPDB automatically when
an exception occurred. IPDB loads all sessions at the moment when an
exception is received.
27. Debugging Tips: Unit Test
The Python unittest module handles the exception on their own. So IPDB
doesn’t work properly outside of unittest class. The %pdb will not launch
when an exception occurs inside unittest.
To debug, you can either :
● Use ipdb.set_trace() manually inside the unittest class.
● $nosetest --ipdb unittest_example.py #it works like %pdb, make sure you install the
plugin by apt install ipdbplugin
28. Debugging Tips: Try-Except Clause
In a typical Try-Except Clause, usually the exception is caught, and traceback
message stops prompting . This is inconvenient to debug.
29. Debugging Tips: Unit Test
Removing try-except clause is tedious. A smarter way is to add raise under the
except scope. raise without argument throws the latest traceback.
30. Debugging Tips: Pickle and Distill
In extreme situations where IPython and IPDB do not work. You can choose to
dump any Python instance by Pickle. And load the instance again in a REPL
environment to debug.
31. Debugging Tips: Pickle and Dill
But in most of cases, we need to dump everything: variables, functions, and
class in order to reproduce the whole environment. Dill helps to dump and
load the whole session of the environment
32. Thank you for your time.
If you have any suggestion and questions. Please contact me ° ͜ʖ ͡ -
Chun-Hao.Chang
ccha97u@gmail.com