Emacs:调试python的方法 [英] Emacs: methods for debugging python
问题描述
我发布在 programmers.stackexchange.com 上,但我认为在SO上可能更合适。
我使用emacs来处理我所有的代码编辑需求。通常,我将使用Mx编译来运行我的测试运行器,我会说,让我约70%的我需要做的,以保持代码的轨道,但最近我一直在想知道如何可能使用Mx pdb在这种情况下,打破断点和检查事情是很好的。
在我的谷歌搜索中,我发现某些事情,表明这是有用的/可能的。然而,我并没有设法让我完全理解它的工作。
我不知道这是否是buildout + appengine的组合,可能会使其变得更加困难,但是当我尝试像
Mx pdb
运行pdb(像这样):/ Users / twillis / projects / hydrant / bin / python / Users / twillis / bin / pdb / users / twillis / projects / hydrant / bin / devappserver / Users / twillis / projects / hydrant / parts / hydrant-app /
其中... / bin / python是解释器buildout,用于为所有的鸡蛋设置路径。
〜/ bin / pdb是简单的脚本调用pdb.main使用当前的python解释器
HellooKitty:消防栓twillis $ cat〜/ bin / pdb
#! / usr / bin / env python
如果__name__ ==__main__:
import sys
sys.version_info
import pdb
pdb.main )
HellooKitty:消防栓twillis $
... / bin / devappserver是dev_appserver脚本这个建筑配方使gae项目和... / parts / hydrant-app是app.yaml的路径
我首先提供了一个提示
p>当前目录是/ Users / twillis / bin /
/ pre>
Cc Cf
没有任何事情发生,但
HellooKitty:消防栓twillis $ ps aux | grep pdb
twillis 469 100.0 1.6 168488 67188 s002 Rs + 1:03 PM 0:52.19 /usr/local/bin/python2.5 / Users / twillis / projects / hydrant / bin / python / Users / twillis / bin / pdb / users / twillis / projects / hydrant / bin / devappserver / Users / twillis / projects / hydrant / parts / hydrant-app /
twillis 477 0.0 0.0 2435120 420 s000 R + 1:05 PM 0:00.00 grep pdb
HellooKitty:消防栓twillis $
某些事情正在发生
Cx [空格]
将报告断点已经设定但我无法让事情发生。
感觉我在这里缺少一些明显的东西。我是吗
那么,emacs中的交互式调试是否值得?是否可以互动调试google appengine应用程序?任何关于我如何得到这个工作的建议?
解决方案GAE的一个具体问题是将STDOUT重定向到浏览器,包括调试器输出。
如 pdb文档,通常的工作流程是在您需要的时候在代码中设置断点。通常你会做
import pdb; pdb.set_trace()
,但在GAE的情况下,您还需要抓住您的STDIN和STDOUT。例如,此代码片段:def set_trace():
import pdb,sys
debugger = pdb.Pdb(stdin = sys .__ stdin__,
stdout = sys .__ stdout__)
debugger.set_trace(sys._getframe()。f_back)
然后在emacs中启动shell,然后从中运行代码:
$ ./bin/devappserver / Users / twillis / projects / hydrant / parts / hydrant-app /
为了方便地访问Emacs中shell交互缓冲区中的正确位置,我使用poptoshell.el(google,我没有足够的声誉来添加另一个链接)
最后,确保你有一个最新版本的python模式,以便它包括pdbtrack功能(你几乎肯定会,但你绝对想要它,因为它提供了在emacs中逐步执行代码的手段) / p>
I posted this on programmers.stackexchange.com, but I figured it may be more appropriate on SO.
I use emacs for all my code edit needs. Typically, I will use M-x compile to run my test runner which I would say gets me about 70% of what I need to do to keep the code on track however lately I've been wondering how it might be possible to use M-x pdb on occasions where it would be nice to hit a breakpoint and inspect things.
In my googling I've found some things that suggest that this is useful/possible. However I have not managed to get it working in a way that I fully understand.
I don't know if it's the combination of buildout + appengine that might be making it more difficult but when I try to do something like
M-x pdb Run pdb (like this): /Users/twillis/projects/hydrant/bin/python /Users/twillis/bin/pdb /Users/twillis/projects/hydrant/bin/devappserver /Users/twillis/projects/hydrant/parts/hydrant-app/
Where .../bin/python is the interpreter buildout makes with the path set for all the eggs.
~/bin/pdb is a simple script to call into pdb.main using the current python interpreter
HellooKitty:hydrant twillis$ cat ~/bin/pdb #! /usr/bin/env python if __name__ == "__main__": import sys sys.version_info import pdb pdb.main() HellooKitty:hydrant twillis$
.../bin/devappserver is the dev_appserver script that the buildout recipe makes for gae project and .../parts/hydrant-app is the path to the app.yaml
I am first presented with a prompt
Current directory is /Users/twillis/bin/ C-c C-f
Nothing happens but
HellooKitty:hydrant twillis$ ps aux | grep pdb twillis 469 100.0 1.6 168488 67188 s002 Rs+ 1:03PM 0:52.19 /usr/local/bin/python2.5 /Users/twillis/projects/hydrant/bin/python /Users/twillis/bin/pdb /Users/twillis/projects/hydrant/bin/devappserver /Users/twillis/projects/hydrant/parts/hydrant-app/ twillis 477 0.0 0.0 2435120 420 s000 R+ 1:05PM 0:00.00 grep pdb HellooKitty:hydrant twillis$
something is happening
C-x [space]
will report that a breakpoint has been set. But I can't manage to get get things going.
It feels like I am missing something obvious here. Am I?
So, is interactive debugging in emacs worthwhile? is interactive debugging a google appengine app possible? Any suggestions on how I might get this working?
解决方案A specific problem with GAE is that it redirects STDOUT to the browser, including debugger output.
As outlined in the pdb documentation, the usual workflow is to set a breakpoint in code at the point you need it. Normally you'd do
import pdb; pdb.set_trace()
, but in the case of GAE, you'll also want to grab your STDIN and STDOUT. For example, this code snippet:def set_trace(): import pdb, sys debugger = pdb.Pdb(stdin=sys.__stdin__, stdout=sys.__stdout__) debugger.set_trace(sys._getframe().f_back)
Then start a shell in emacs and run your code from there:
$ ./bin/devappserver /Users/twillis/projects/hydrant/parts/hydrant-app/
For easy access to the right place in a shell-interaction buffer in emacs, I use poptoshell.el (google it, I don't have sufficient reputation to add another link)
Finally, make sure you have a recent enough version of python-mode such that it includes pdbtrack functionality (you almost certainly do, but you definitely want it, as it provides the means to step through code in emacs).
这篇关于Emacs:调试python的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!