我常用的 Python 调试工具 - 博客 - 伯乐在线
.ckrating_highly_rated {background-color:#FFFFCC !important;}
.ckrating_poorly_rated {opacity:0.6;filter:alpha(opacity=60) !important;}
.ckrating_hotly_debated {background-color:#FFF0F5 !important;}
.syntaxhighlighter,
.syntaxhighlighter a,
.syntaxhighlighter div,
.syntaxhighlighter code,
.syntaxhighlighter table,
.syntaxhighlighter table td,
.syntaxhighlighter table tr,
.syntaxhighlighter table tbody,
.syntaxhighlighter table thead,
.syntaxhighlighter table caption,
.syntaxhighlighter textarea {
font-size: 12px !important; /* Set the font size in pixels */
font-family: "Consolas", "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; /* Set the font type */
}
.syntaxhighlighter table caption {
/* For Title(Caption) */
font-size: 14px !important; /* Set the font size in pixels */
font-family: "Consolas", "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; /* Set the font type */
}
.syntaxhighlighter.nogutter td.code .line {
/* Set the left padding space when no-gutter in ver. 3.0 */
padding-left: 3px !important;
}
.syntaxhighlighter {
/* For Chrome/Safari(WebKit) */
/* Hide the superfluous vertical scrollbar in ver. 3.0 */
overflow-y: hidden !important;
padding: 1px !important;
}
.widget-area.syntaxhighlighter a,
.widget-area.syntaxhighlighter div,
.widget-area.syntaxhighlighter code,
.widget-area.syntaxhighlighter table,
.widget-area.syntaxhighlighter table td,
.widget-area.syntaxhighlighter table tr,
.widget-area.syntaxhighlighter table tbody,
.widget-area.syntaxhighlighter table thead,
.widget-area.syntaxhighlighter table caption,
.widget-area.syntaxhighlighter textarea {
/* For Widget */
font-size: 14px !important; /* Set the font size in pixels */
font-family: "Consolas", "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; /* Set the font type */
}
.widget-area table caption {
/* For Widget */
/* For Title(Caption) */
font-size: 10px !important; /* Set the font size in pixels */
font-family: "Consolas", "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; /* Set the font type */
}
div.container{
width:inherit
}
*::selection { background: #0099cc; }
*::-moz-selection { background: #0099cc; }
body { background: #ffffff }
a, .tabs ul.nav li a:hover, .tabs ul.nav li.active a, .dropcap, .toggle.hover .toggle-title, li.comment cite a:hover, h3.widget-title, .post-meta .meta-title:hover, .the-latest a:hover h4, .aw_socialcounter_widget li a:hover, .aw_tabbed_widget #tab-latest-comments a:hover { color: #0099cc; }
a:hover { color: #b30000; }
input:focus, textarea:focus { border-color: #0099cc; }
#searchsubmit, .highlight, .aw_tabbed_widget .tabs ul.nav li.active a, footer .aw_tabbed_widget .tabs ul.nav li.active a, #top .aw_tabbed_widget .tabs ul.nav li.active a, .aw_tabbed_widget .tabs ul.nav li a:hover, footer .aw_tabbed_widget .tabs ul.nav li a:hover, #top .aw_tabbed_widget .tabs ul.nav li a:hover, .aw_twitter_widget .twitter-icon, .testimonial-icon, #top-closed:hover, .flex-control-nav a:hover, .flex-control-nav a.flex-active { background-color: #0099cc; }
.submit { background-color: #0099cc; border-color: #007399; }
.submit:hover { background-color: #b30000; border-color: #860000; }
#searchsubmit:hover { background-color: #b30000; }
.toggle.hover .toggle-icon { border-top-color: #0099cc; }
.toggle.hover.active .toggle-icon { border-bottom-color: #0099cc; }
.flex-direction-nav li .flex-prev:hover { border-right-color: #0099cc; }
.flex-direction-nav li .flex-next:hover { border-left-color: #0099cc; }
@media only screen and (min-width: 768px) and (max-width: 959px) {
.aw_tabbed_widget .tabs ul.nav li a:hover, .tabs ul.nav li.active a { color: #0099cc; }
}
@media screen and (max-width: 767px) {
.tabs ul.nav li a:hover, .tabs ul.nav li.active a { color: #0099cc; }
}
我常用的 Python 调试工具
以下是我做调试或分析时用过的工具的一个概览。如果你知道有更好的工具,请在评论中留言,可以不用很完整的介绍。
日志
没错,就是日志。再多强调在你的应用里保留足量的日志的重要性也不为过。你应当对重要的内容打日志。如果你的日志打的足够好的话,单看日志你就能发现问题所在。那样可以节省你大量的时间。
如果一直以来你都在代码里乱用 print 语句,马上停下来。换用logging.debug。以后你还可以继续复用,或是全部停用等等。
跟踪
有时更好的办法是看执行了哪些语句。你可以使用一些IDE的调试器的单步执行,但你需要明确知道你在找那些语句,否则整个过程会进行地非常缓慢。
标准库里面的trace模块,可以打印运行时包含在其中的模块里所有执行到的语句。(就像制作一份项目报告)
python -mtrace –trace script.py
这会产生大量输出(执行到的每一行都会被打印出来,你可能想要用grep过滤那些你感兴趣的模块).
比如:
python -mtrace –trace script.py | egrep '^(mod1.py|mod2.py)'
调试器
以下是如今应该人尽皆知的一个基础介绍:
import pdb
pdb.set_trace() # 开启pdb提示
或者
try:
(一段抛出异常的代码)
except:
import pdb
pdb.pm() # 或者 pdb.post_mortem()
或者(输入 c 开始执行脚本)
python -mpdb script.py
在输入-计算-输出循环(注:REPL,READ-EVAL-PRINT-LOOP的缩写)环境下,可以有如下操作:
- c or continue
- q or quit
- l or list, 显示当前步帧的源码
- w or where,回溯调用过程
- d or down, 后退一步帧(注:相当于回滚)
- u or up, 前进一步帧
- (回车), 重复上一条指令
其余的几乎全部指令(还有很少的其他一些命令除外),在当前步帧上当作python代码进行解析。
如果你觉得挑战性还不够的话,可以试下smiley,-它可以给你展示那些变量而且你能使用它来远程追踪程序。
更好的调试器
pdb的直接替代者:
ipdb(easy_install ipdb) – 类似ipython(有自动完成,显示颜色等)
pudb(easy_install pudb) – 基于curses(类似图形界面接口),特别适合浏览源代码
远程调试器
安装方式:
sudo apt-get install winpdb
用下面的方式取代以前的pdb.set_trace():
import rpdb2
rpdb2.start_embedded_debugger("secretpassword")
现在运行winpdb,文件-关联
不喜欢Winpdb?也可以直接包装PDB在TCP之上运行!
这样做:
import loggging class Rdb(pdb.Pdb):
"""
This will run pdb as a ephemeral telnet service. Once you connect no one
else can connect. On construction this object will block execution till a
client has connected. Based on https://github.com/tamentis/rpdb I think ... To use this:: Rdb(4444).set_trace() Then run: telnet 127.0.0.1 4444
"""
def __init__(self, port=0):
self.old_stdout = sys.stdout
self.old_stdin = sys.stdin
self.listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.listen_socket.bind(('0.0.0.0', port))
if not port:
logging.critical("PDB remote session open on: %s", self.listen_socket.getsockname())
print >> sys.__stderr__, "PDB remote session open on:", self.listen_socket.getsockname()
sys.stderr.flush()
self.listen_socket.listen(1)
self.connected_socket, address = self.listen_socket.accept()
self.handle = self.connected_socket.makefile('rw')
pdb.Pdb.__init__(self, completekey='tab', stdin=self.handle, stdout=self.handle)
sys.stdout = sys.stdin = self.handle def do_continue(self, arg):
sys.stdout = self.old_stdout
sys.stdin = self.old_stdin
self.handle.close()
self.connected_socket.close()
self.listen_socket.close()
self.set_continue()
return 1 do_c = do_cont = do_continue def set_trace():
"""
Opens a remote PDB on first available port.
"""
rdb = Rdb()
rdb.set_trace()
只想要一个REPL环境?试试IPython如何?
如果你不需要一个完整齐全的调试器,那就只需要用一下的方式启动一个IPython即可:
import IPython
IPython.embed()
标准linux工具
我常常惊讶于它们竟然远未被充分利用。你能用这些工具解决很大范围内的问题:从性能问题(太多的系统调用,内存分配等等)到死锁,网络问题,磁盘问题等等。
其中最有用的是最直接的strace,只需要运行 sudo strace -p 12345 或者 strace -f 指令(-f 即同时追踪fork出来的子进程),这就行了。输出一般会非常大,所以你可能想要把它重定向到一个文件以便作更多的分析(只需要加上 &> 文件名)。
再就是ltrace,有点类似strace,不同的是,它输出的是库函数调用。参数大体相同。
还有lsof 用来指出你在ltrace/strace中看到的句柄数值的意义。比如:
lsof -p 12345
更好的跟踪
使用简单而可以做很多事情-人人都该装上htop!
sudo apt-get install htop
sudo htop
现在找到那些你想要的进程,再输入:
s - 代表系统调用过程(类似strace)
L - 代表库调用过程(类似ltrace)
l - 代表lsof
监控
没 有好的持续的服务器监控,但是如果你曾遇到一些很诡异的情况,诸如为什么一切都运行的那么慢,那些系统资源都干什么去了,。。。等这些问题,想弄明白却又 无处下手之际,不必动用iotop,iftop,htop,iostat,vmstat这些工具,就用dstat吧!它可以做之前我们提过的大部分工作可 以做的事情,而且也许可以做的更好!
它会用一种紧凑的,代码高亮的方式(不同于iostat,vmstat)向你持续展示数据,你还经常可以看到过去的数据(不同于iftop,iostop,htop)。
只需运行:
dstat --cpu --io --mem --net --load --fs --vm --disk-util --disk-tps --freespace --swap --top-io --top-bio-adv
很可能有一种更简短的方式来写上面这条命令,
这是一个相当复杂而又强大的工具,但是这里我只提到了一些基本的内容(安装以及基础的命令)
sudo apt-get install gdb python-dbg
zcat /usr/share/doc/python2.7/gdbinit.gz > ~/.gdbinit
用python2.7-dbg 运行程序:
sudo gdb -p 12345
现在使用:
bt - 堆栈跟踪(C 级别)
pystack - python 堆栈跟踪,不幸的是你需要有~/.gdbinit 并且使用python-dbg
c - 继续
发生段错误?用faulthandler !
python 3.3版本以后新增的一个很棒的功能,可以向后移植到python2.x版本。只需要运行下面的语句,你就可以大抵知道什么原因引起来段错误。
import faulthandler
faulthandler.enable()
内存泄露
嗯,这种情况下有很多的工具可以使用,其中有一些专门针对WSGI的程序比如Dozer,但是我最喜欢的当然是objgraph。使用简单方便,让人惊讶!
它没有集成WSGI或者其他,所以你需要自己去发现运行代码的方法,像下面这样:
import objgraph
objs = objgraph.by_type("Request")[:15]
objgraph.show_backrefs(objs, max_depth=20, highlight=lambda v: v in objs,
filename="/tmp/graph.png")
Graph written to /tmp/objgraph-zbdM4z.dot (107 nodes)
Image generated as /tmp/graph.png
你会得到像这样一张图(注意:它非常大)。你也可以得到一张点输出。
内存使用
有时你想少用些内存。更少的内存分配常常可以使程序执行的更快,更好,用户希望内存合适好用)
有许多可用的工具,但在我看来最好用的是pytracemalloc。与其他工具相比,它开销非常小(不需要依赖于严重影响速度的sys.settrace)而且输出非常详尽。但安装起来比较痛苦,你需要重新编译python,但有了apt,做起来也非常容易。
只需要运行这些命令然后去吃顿午餐或者干点别的:
apt-get source python2.7
cd python2.7-*
wget? https://github.com/wyplay/pytracemalloc/raw/master/python2.7_track_free_list.patch
patch -p1 < python2.7_track_free_list.patch
debuild -us -uc
cd ..
sudo dpkg -i python2.7-minimal_2.7*.deb python2.7-dev_*.deb
接着安装pytracemalloc (注意如果你在一个virtualenv虚拟环境下操作,你需要在重新安装python后再次重建 – 只需要运行 virtualenv myenv)
pip install pytracemalloc
现在像下面这样在代码里包装你的应用程序
import tracemalloc, time
tracemalloc.enable()
top = tracemalloc.DisplayTop(
5000, # log the top 5000 locations
file=open('/tmp/memory-profile-%s' % time.time(), "w")
)
top.show_lineno = True
try:
# code that needs to be traced
finally:
top.display()
输出会像这样:
2013-05-31 18:05:07: Top 5000 allocations per file and line
#1: .../site-packages/billiard/_connection.py:198: size=1288 KiB, count=70 (+0),
average=18 KiB
#2: .../site-packages/billiard/_connection.py:199: size=1288 KiB, count=70 (+0),
average=18 KiB
#3: .../python2.7/importlib/__init__.py:37: size=459 KiB, count=5958 (+0),
average=78 B
#4: .../site-packages/amqp/transport.py:232: size=217 KiB, count=6960 (+0),
average=32 B
#5: .../site-packages/amqp/transport.py:231: size=206 KiB, count=8798 (+0),
average=24 B
#6: .../site-packages/amqp/serialization.py:210: size=199 KiB, count=822 (+0),
average=248 B
#7: .../lib/python2.7/socket.py:224: size=179 KiB, count=5947 (+0), average=30
B
#8: .../celery/utils/term.py:89: size=172 KiB, count=1953 (+0), average=90 B
#9: .../site-packages/kombu/connection.py:281: size=153 KiB, count=2400 (+0),
average=65 B
#10: .../site-packages/amqp/serialization.py:462: size=147 KiB, count=4704
(+0), average=32 B
…
很美,不是吗?
补充:更多有关调试的内容见这里。
原文链接: Ionel
Cristian Mărieș 翻译: 伯乐在线 - 高磊
译文链接:
http://blog.jobbole.com/51062/
[ 转载必须在正文中标注并保留原文链接、译文链接和译者等信息。]
关于作者: 高磊

关注后台技术,渴望将技术做到极致
新浪微博@kaulie
我常用的 Python 调试工具 - 博客 - 伯乐在线的更多相关文章
- python学习博客地址集合。。。
python学习博客地址集合... 老师讲课博客目录 http://www.bootcdn.cn/bootstrap/ bootstrap cdn在线地址 http://www.cnblogs. ...
- 用Python编写博客导出工具
用Python编写博客导出工具 罗朝辉 (http://kesalin.github.io/) CC 许可,转载请注明出处 写在前面的话 我在 github 上用 octopress 搭建了个人博 ...
- 原创翻译-值得关注的10个python语言博客
原文链接 原文链接的网页感觉网络不是很好,不容易上.我在这里就给大家做个翻译吧. 大家好,还记得我当时学习python的时候,我一直努力地寻找关于python的博客,但我发现它们的数量很少.这也是我建 ...
- 值得关注的10个python语言博客(转)
大家好,还记得我当时学习python的时候,我一直努力地寻找关于python的博客,但我发现它们的数量很少.这也是我建立这个博客的原因,向大家分享我自己学到的新知识.今天我向大家推荐10个值得我们关注 ...
- 值得关注的10个python语言博客
大家好,还记得我当时学习python的时候,我一直努力地寻找关于python的博客,但我发现它们的数量很少.这也是我建立这个博客的原因,向大家分享我自己学到的新知识.今天我向大家推荐10个值得我们关注 ...
- Python大婶博客汇总
Python大神金星 博客:http://www.cnblogs.com/jin-xin/articles/7459977.html
- 【Python】博客信息爬取-微信消息自动发送
1.环境安装 python -m pip install --upgrade pip pip install bs4 pip install wxpy pip install lxml 2.博客爬取及 ...
- python实战博客
2018-10-31 更新Logging日志记录以及异常捕获 感谢廖大教程.Python实战 直接在闲置的服务器上开发.阿里云Centos 6.8 64位. 1 搭建开发环境 Python 环境是Py ...
- python十个博客
1.首先第一个肯定是毋庸置疑的Planet Python这是最出名的python博客其中之一,快去看看,我希望你能找到对你有用的东西,昨天它还更新了呢! 2.第二个博客是lucumr,博主是flask ...
随机推荐
- 从function的定义看JavaScript的预加载
在JavaScript中定义一个函数,有两种写法: function ftn(){} // 第一种 var ftn = function(){} // 第二种 有人说,这两种写法是完全等价的.但是在解 ...
- 常用Build-in Keywords
1. Variables |- Set variable |- Create list |- Evaluate |- Get Variable Value 2. Conditional |- Run ...
- pg删除账号,权限的回收问题
在pg中删除账号时,一般不能直接删除账号,要先将该账号上所有的对应权限收回,但往往这一步是比较繁琐的,可能当时赋权的对象类型很多,对象也比较多,虽然可以通过sql按照类型来收回针对整个schema的所 ...
- win7,Ubuntu 12.04 双系统修改启动项顺序三方法
修改启动项顺序的三种方法 本文所涉及的方法都是在Ubuntu的安装时将引导加载程序grub安装在了整个硬盘(即MBR内),即开机以grub引导. 方法1在Ubuntu终端下输入:sudo mv /et ...
- 使用traefik作为kubernetes的ingress
目录 说明 部署 创建一个独立的命名空间 配置rbac授权 配置secret 创建一个configmap用于存放traefik的配置文件 配置traefik的deployment文件 配置服务 通过p ...
- cuckoo 安装
最新 https://www.jianshu.com/p/f623fa0bebf9 http://www.freebuf.com/articles/system/123816.html http:// ...
- PHP变量的传值和引用
问题: 1.PHP变量的存储.取值方式如何? 2.变量赋值时,普通传值和引用传值分别是什么意思?有何区别? 3.unset被赋值的变量会对两种赋值后原值和新值的影响? 变量的存储.取值形式: 变量 ...
- NAT—网络地址转换
参考链接:http://www.qingsword.com/qing/745.html 视频链接: 一.什么是NAT? NAT --- Network Address Translation 也就是 ...
- 高质量API网关组件实现
PI网关组件的作用? 1.网关直接代替MVC当中的Controller层,减少编码量提高开发效率 2.统一API接口的出入参格式,提高API的友好性 3.自动检测API接口规范,提高接口的质量 4.统 ...
- [转载]asp.net mvc: why is Html.CheckBox generating an additional hidden input
http://stackoverflow.com/questions/2697299/asp-net-mvc-why-is-html-checkbox-generating-an-additional ...