debug ipython notebook with breakpoint
2015-12-08 09:26
435 查看
http://www.cs.ucl.ac.uk/scipython/notebooks/02-BreakPoints.html
Debugging
Debugging is the process of finding errors in a programvery important part of programming
can take considerible time
mastering debugging techniques can save you a lot of time
Literature
Python docs on errors and exceptionshttp://docs.python.org/2/tutorial/errors.html
Overview
Introduction/ MotivationDebugging in with IPython
%debug
Exercise
Debugging with break-points
Exercise
Debugging with the logger module
Exercise
Nose tests
Exercise
Debugging with break-points
Break points are a traditional method of inspecting how a program works. Break points are points in a program where the debugger stops each time the program flow passes these points. Ifthe program is not run with a debugger, break-points are ignored. It can be used to understand how data is changed as the program runs. It is an essential tool in compiled languages, because you cannot manipulate data live in these languages, only view them.
In many programming languages break-points are handled by the IDE and a single click next to the line number in the editor switches them on or off.
ipdb
module
In the following examples I am going to use the Ipython debugging module, which wraps the orignal pdbcommands
(i.e. uses the same commands as the standard
pdb). To install
ipdbjust
type
pip install ipdb
in your terminal. If that fails for some reason, you can change the
importline
below to
import pdb
And you should be able to get the same things done. However, the
ipdbprovides
nice syntax highlighting and generally a nicer view of the stack trace.
set_trace()
In python, break-points are defined with the set_trace()function:
Unfortunately the ipdb is broken in the IPython notebook at the moment. Therefore I am showing you this in the terminal. The following code is saved out as
scripts/01.py.
Open a terminal and run it as
python scripts/01.py.
# scripts/01.py import ipdb as pdb def functionWithBreakpoint(): number = 4 number += 3 pdb.set_trace() number += 2 return number print functionWithBreakpoint()
We can play a little bit around with it and try out the commands we know already from the
%debugmagic
we introduced before.
Commands
(courtesy of http://georgejhunt.com/olpc/pydebug/pydebug/ipdb.html)s(tep) -- Execute the current line, stop at the first possible occasion (either in a function that is called or in the current function).
n(ext) -- Continue execution until the next line in the current function is reached or it returns.
unt(il) -- Continue execution until the line with a number greater than the current one is reached or until the current frame returns.
r(eturn) -- Continue execution until the current function returns.
c(ont(inue)) -- Continue execution, only stop when a breakpoint is encountered.
Stepping though several break-points
# scripts/02.py import ipdb as pdb def functionWithBreakpoint(): number = 0 for i in range(10): number += 2 pdb.set_trace() numnber -= 1 pdb.set_trace() return number print functionWithBreakpoint()
相关文章推荐
- Python基础——timeit模块
- leetcode Binary Tree Right Side View python
- python培训Day7 随笔
- python 批量下载图片
- Python使用pygame模块编写俄罗斯方块游戏的代码实例
- 一波神奇的Python语句、函数与方法的使用技巧总结
- 使用Python编写简单的画图板程序的示例教程
- 笔记:Python进行数据库文件导出备份
- python-48:直接使用POST方法登录网站
- python-49: 爬虫模拟登陆源码
- 12.7 coding-python-mongo相关代码笔记
- 《Python核心编程》第10章 习题
- python语法笔记(三)
- Python字典里面创建字典
- leetcode Linked List Cycle II python
- leetcode Linked List Cycle python
- python对象内存分析
- Xcode搭建Python编译环境
- 【Python】Java转Python的一些水土不服
- leetcode LRU Cache python