Eclipse+PyDev中的Python调试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2704932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 14:39:17  来源:igfitidea点击:

Python debugging in Eclipse+PyDev

pythoneclipsepydev

提问by G?khan Sever

I try Eclipse+PyDev pair for some of my work. (Eclipse v3.5.0 + PyDev v1.5.6) I couldn't find a way to expose all of my variables to the PyDev console (Through PyDev console -> Console for current active editor option) I use a simple code to describe the issue. When I step-by-step go through the code I can't access my "x" variable from the console. It is viewed on Variables tab, but that's not really what I want.

我在一些工作中尝试使用 Eclipse+PyDev 对。(Eclipse v3.5.0 + PyDev v1.5.6)我找不到将所有变量公开到 PyDev 控制台的方法(通过 PyDev 控制台 -> 当前活动编辑器选项的控制台)我使用一个简单的代码来描述问题. 当我逐步完成代码时,我无法从控制台访问我的“x”变量。它在变量选项卡上查看,但这并不是我真正想要的。

Any help is appreciate.

任何帮助表示赞赏。

See my screenshot for better description:

请参阅我的屏幕截图以获得更好的描述:

alt text

替代文字

EDIT:

编辑:

Assume adding a simple func like:

假设添加一个简单的函数,如:

def myfunc(x):
    return x**x

When I debug with the function added in the code I can access myfunc from the console easily. (Type myfunc and it will be available after this automatic execution:

当我使用代码中添加的函数进行调试时,我可以轻松地从控制台访问 myfunc。(键入 myfunc 它将在此自动执行后可用:

>>> from part2.test import myfunc
>>> myfunc

Then when I do myfunc(5) it acts just like in the Python interpreter. It would be so useful to access variables in the similar fashion for debugging my code. I have big arrays and I do various tests and operations during debug process. Like: Get my x and do x.sum(), later do x[::10], or transpose operate with other arrays observe results, experiment etc...

然后当我执行 myfunc(5) 时,它的作用就像在 Python 解释器中一样。以类似的方式访问变量以调试我的代码会非常有用。我有大数组,我在调试过程中进行了各种测试和操作。如:获取我的 x 并执行 x.sum(),稍后执行 x[::10],或与其他数组进行转置操作,观察结果、实验等...

Hope there will be a better solution.

希望会有更好的解决方案。

回答by Fabio Zadrozny

Update:

更新:

In the latest PyDev versions, it's possible to right-click a frame in the stack and select PyDev > Debug console to have the interactive console with more functions associated to a context during a debug session.

在最新的 PyDev 版本中,可以右键单击堆栈中的框架并选择 PyDev > 调试控制台,以便在调试会话期间具有与上下文相关联的更多功能的交互式控制台。



Unfortunately, the actual interactive console, which would be the preferred way of playing with code (with code-completion, etc -- http://pydev.org/manual_adv_interactive_console.html) has no connection to a debug session right now (this is planned but still not implemented).

不幸的是,实际的交互式控制台,这将是玩代码的首选方式(使用代码完成等 - http://pydev.org/manual_adv_interactive_console.html)现在没有与调试会话的连接(这是计划但仍未实施)。

Still, with the 'simpler' console available, you are still able to interactively inspect and play with the variables available in a breakpoint scope: http://pydev.org/manual_adv_debug_console.html(which is the same as you'd have with pdb -- it's just a matter of typing code in the available console after a breakpoint is hit).

尽管如此,使用“更简单”的控制台,您仍然可以交互式地检查和使用断点范围内可用的变量:http: //pydev.org/manual_adv_debug_console.html(这与您使用的相同pdb——这只是在断点被击中后在可用控制台中输入代码的问题)。

Cheers,

干杯,

Fabio

法比奥

回答by stw_dev

For this sort of exploratory debugging I like to use pdb, the batteries-included debugger. I haven't used it inside PyDev so I don't know how it would all fit together. My guess is it will do what you expect it to. An example of its usage:

对于这种探索性调试,我喜欢使用 pdb,包含电池的调试器。我没有在 PyDev 中使用过它,所以我不知道它是如何组合在一起的。我的猜测是它会做你所期望的。其用法示例:

import pdb

def myfunc(x):
    pdb.set_trace()
    return x**x

This will break right before executing the return statement, and it allows you to use full Pythonic statements to figure out what's going on. I use it like an interactive print statement: setting the place where I want to dive in, examining values and figuring results, and stepping through to watch it happen. Perhaps this is a lazy way of debugging, but sometimes you need more information before you can make less-lazy decisions :-)

这将在执行 return 语句之前中断,它允许您使用完整的 Pythonic 语句来弄清楚发生了什么。我使用它就像一个交互式打印语句:设置我想要潜入的地方,检查值和计算结果,并逐步观察它的发生。也许这是一种懒惰的调试方式,但有时您需要更多信息才能做出不那么懒惰的决定:-)

The page I usually reference is at Python Conquers The Universewhich also links a few other sources of information.

我通常参考的页面是Python Conquers The Universe,该页面还链接了一些其他信息来源。