pdb: Using the Python debugger in Django
来源:互联网 发布:qq三国js技能名 编辑:程序博客网 时间:2024/06/13 11:20
A couple conversations I had at DjangoCon last week reminded me that I’d never gotten around to finishing this little post.
Since I started taking my first iOS development baby steps last year, I’ve become quite dependent on breakpoints and stepping as part of my Objective-C debugging workflow.
…And yet, in Python/Django — the stack I spend most of my time working with — I’d stuck to the poor habit of throwing print
statements around any found exceptions — for example, to figure out what lies in request.POST
for a Django view. And then I came across and started tinkering with pdb
.
Here’s a brief overview on how to rock your Python debugging workflow by using an actual interactive debugger. The example below highlights the most basic of usecases, but it’s a good start.
Using pdb with Django
Say, you have a view that looks like this:
from django.http import HttpResponse
def default(request): # completely innocuous variables foo = 1 bar = 0 # completely innocuous division ni = foo/bar return HttpResponse("Foo says %d" % ni, mimetype="text/plain")
And say, you run your local development server with:
manage.py runserver
When accessed, this view will generally throw you something like this:
By replacing the manage.py runserver
command with one wrapped in PDB, we can start digging a little bit deeper into this problem view.
python -m pdb manage.py runserver
You will notice that the shell hangs on a (Pdb)
prompt. Enter c
for “continue” (command reference here) and hit enter. (You’ll also notice that control-c restartsrunserver
, since the default PDB behavior is to restart a script after it ends. Just hit control-c a few times to break PDB’s execution loop when you want to kill the server.)
You’ll notice that running under PDB doesn’t do anything by itself.
You’ll need to set a breakpoint to actually tell PDB where to stop execution. Inserting the following line of code causes PDB to break when it executes — which, in turn, triggers the debugging shell.
import pdb; pdb.set_trace()
In our example, we’d like to inspect where our exception is getting thrown, so we’ll throw it right before the offending line of code:
from django.http import HttpResponse
def default(request): # completely innocuous variables foo = 1 bar = 0 # well, this is where our error is, so let's trace it import pdb; pdb.set_trace() ni = foo/bar return HttpResponse("Foo says %d" % ni, mimetype="text/plain")
Congrats! Your browser is now waiting for your view to finish, your view is waiting (at the breakpoint) for PDB to finish, and PDB is waiting for you in the shell.
Doing things in PDB
At this point, the PDB shell acts like the normal python
/ipython
shell. You can look at variables right in the scope of the breakpoint.
When working with a breakpoint, you can even screw around with local variables to try and find a fix, but note that only the last line you execute is remembered when you continue
. You can still execute several statements by using the semicolon:
原文网址链接:https://mike.tig.as/blog/2010/09/14/pdb/
- pdb: Using the Python debugger in Django
- pdb — The Python Debugger
- pdb — The Python Debugger
- The python debugger调试(PDB)的简介
- 使用PDB(The Python Debugger)命令行调试Python代码
- pdb — The Python Debugger(Python调试器)
- Notes on Python Debugger pdb
- Using the Debugger
- Using the Remote Debugger
- Using the SoftICE Debugger in a Virtual Machine
- [Python] The project structure in Django
- Using the Windows Debugger: Some Handy/Fun/Clever Debugger Commands
- PDB创建之Creating a PDB Using the Seed
- Using the Microsoft Visual Studio 2005 Debugger
- Using the Chrome Debugger Tools, part 3: The Sources Tab
- UIViewAlertForUnsatisfiableConstraints to catch this in the debugger
- [debug] debugging in python with pdb
- 使用调试器类(Using the Debugger Class)
- java中static{}语句块详解
- Java 框架之Hibernate (三、Hibernate一对多及多对多的关系分析)
- 使用Powerdesigner从sql server数据库生成pdm
- 解决 iframe 在 iPad 上无法滚动的问题
- Hibernate HQL查询 插入 更新(update)实例
- pdb: Using the Python debugger in Django
- 解决瀑布流recyclerview的闪烁移动问题
- Extjs grid中的选择框默认选择时为对号,改为叉号
- Win10 下面PowerShell 运行python2.7
- 在modal viewcontroller外点击屏幕dismiss
- 一个把json数据批量插入SQL SERVER 的Python脚本
- MVC @Html控件 TextBoxFor MVC 控件 加属性
- 直白解读网络编程第二节_进程(入门)
- 【背包专题】A