简体   繁体   English

在 Python 中,如何使用交互式命令行(和可视断点?)进行调试

[英]In Python, how do I debug with an interactive command line (and visual breakpoints?)

I've recently moved from Matlab to Python.我最近从 Matlab 转移到 Python。 Python is a much better language (from the point of view of a computer scientist), but Python IDEs all seem to lack one important thing: Python 是一种更好的语言(从计算机科学家的角度来看),但是 Python IDE 似乎都缺少一个重要的东西:

A proper interactive debugger.一个合适的交互式调试器。

I'm looking for:我在找:

  • The ability to set breakpoints graphically by clicking next to a line of code in the editor.通过在编辑器中的一行代码旁边单击来以图形方式设置断点的能力。

  • The ability to run ANY CODE while stopped in the debugger, including calling functions from my code, showing new windows, playing audio, etc.在调试器中停止时运行任何代码的能力,包括从我的代码调用函数、显示新窗口、播放音频等。

  • When an error occurs, the debugger should automatically open an interactive console at the error line.发生错误时,调试器应自动在错误行打开交互式控制台。

  • Once done with the interactive console, you can resume normal execution.完成交互式控制台后,您可以恢复正常执行。

Matlab has all these features and they work incredibly well, but I can't find them anywhere in Python tools. Matlab 具有所有这些功能,并且它们的运行非常好,但是我在 Python 工具中找不到它们。

I've tried:我试过了:

  • PyCharm: the interactive console is clunky, often fails to appear, and crashes all the time (I've tried several different versions and OSs). PyCharm:交互式控制台笨拙,经常无法出现,并且一直崩溃(我尝试了几个不同的版本和操作系统)。

  • IPython: can't set breakpoints -Launching a Python console programatically: you have to stop your code, insert an extra line of code, and run again from the beginning to do this. IPython:无法设置断点 - 以编程方式启动 Python 控制台:您必须停止代码,插入额外的代码行,然后从头开始再次运行才能执行此操作。 Plus, you can't access functions already imported without re-importing them.此外,如果不重新导入,您将无法访问已导入的函数。

Being able to debug and fix problems THE FIRST TIME THEY APPEAR is very important to me, as I work in programs that often take dozens of minutes to re-run (computational neuroscience).能够在问题第一次出现时进行调试和修复对我来说非常重要,因为我在处理通常需要几十分钟才能重新运行的程序(计算神经科学)。

CONCLUSION: there is NO way to do all of these in Python at the moment.结论:目前没有办法在 Python 中完成所有这些。 Let us hope that PyLab development accelerates.让我们希望 PyLab 的发展加速。

There are many Python IDEs . 许多Python IDE That was a topic here: What IDE to use for Python? 这是一个主题: 用于Python的IDE是什么?

  • "The ability to set breakpoints graphically by clicking next to a line of code in the editor." “通过单击编辑器中的一行代码旁边以图形方式设置断点的功能。”

PyDev has this. PyDev有这个。 Double-click in the gray margin bar. 双击灰色边距栏。

  • "The ability to run ANY CODE while stopped in the debugger, including calling functions from my code, showing new windows, playing audio, etc." “能够在调试器中停止运行任何代码,包括从我的代码调用函数,显示新窗口,播放音频等。”

PyDev has this . PyDev有这个 It's not the only one. 这不是唯一的一个。 PyScripter's stated features seem to include this. PyScripter的声明功能似乎包含了这一点。

  • "When an error occurs, the debugger should automatically open an interactive console at the error line." “发生错误时,调试器应自动在错误行打开交互式控制台。”

PyDev does this. PyDev这样做。 (I think. Or at worst do you need to double-click on the console message that states the error's location in the code?) (我想。或者在最坏的情况下,您是否需要双击控制台消息,指出错误在代码中的位置?)

  • "Once done with the interactive console, you can resume normal execution." “完成交互式控制台后,您可以恢复正常执行。”

PyDev has this. PyDev有这个。 It's called "resume". 它被称为“简历”。 It's what the green "play" triangle in a toolbar does. 它就是工具栏中绿色的“游戏”三角形。 Some other software calls this feature "continue". 其他一些软件称此功能为“继续”。

At the top of your code, write 在代码的顶部,写

import pdb

And within your code, use the following statement wherever you want to debug. 在您的代码中,在您想要调试的任何地方使用以下语句。

pdb.set_trace()

You will have an interactive shell thus, whenever the set_trace() statement is met. 每当满足set_trace()语句时,您将拥有一个交互式shell。

You can then use step(s) , next(n) , continue(c) and so on to check the execution flow, and print values of variables like print var 然后,您可以使用step(s)next(n)continue(c)等来检查执行流程,并打印变量值,例如print var

For more details on pdb, refer here 有关pdb的更多详细信息, 请参阅此处

I've been searching for the same, but unfortunately Python IDEs are not as well-featured as Matlab's at this point. 我一直在寻找相同的,但不幸的是,Python IDE在这一点上并不像Matlab那样功能齐全。 For scientific programming, you will also want graphics/plotting to run in an entirely different thread, so IPython integration is essential. 对于科学编程,您还希望图形/绘图在完全不同的线程中运行,因此IPython集成至关重要。 As far as I can tell, the Matlab IDE feature to change the workspace from the debugger, which then affects code running subsequently, is quite unique. 据我所知,Matlab IDE功能从调试器更改工作区,然后影响随后运行的代码,这是非常独特的。 Each of the features exist in some IDE, but none exist in all: 某些 IDE中存在每个功能,但并不存在:

  • Spyder has good integration with scientific tools, but its debugging is limited to the built-in pdb , which lacks the requirement to execute any code and have this code affect the namespace after continuing. Spyder与科学工具有很好的集成,但它的调试仅限于内置的pdb ,它缺乏执行任何代码的要求,并且在继续之后使这些代码影响命名空间。
  • PyDev and PyCharm , and quite a few others, have decent debugging features, but I don't think it has good integration with scientific tools. PyDevPyCharm以及其他一些人都有不错的调试功能,但我认为它与科学工具没有很好的集成。 That means, if you plot, you lose access to your prompt. 这意味着,如果您进行绘图,则会失去对提示的访问权限。 Not good. 不好。

As far as I've experienced, the closest comes Wing IDE . 据我所知,最接近Wing IDE It is a propietry product, but if you're making the transition from Matlab 89$/year for non-commercial-use should be acceptable (you can evaluate it first). 它是一种支持产品,但是如果您从Matlab转换89美元/年用于非商业用途应该是可接受的(您可以先评估它)。 But for me, I've ultimately settled to alter my workflow, and not using any sophisticated IDE at all. 但对我来说,我最终决定改变我的工作流程,而不是使用任何复杂的IDE。 When I looked was some years ago, so perhaps the situation has improved. 几年前我看的时候,情况也许有所改善。

You might also be interested in this article from April 2013, Evaluating IDEs for Scientific Python . 您可能还会对2013年4月的“ 为科学Python评估IDE”这篇文章感兴趣。 It doesn't really reach a conclusion either. 它也没有真正得出结论。

You can do all this in the iPython Notebook. 您可以在iPython Notebook中完成所有这些操作。 Use the magic command %pdb to stop on error. 使用magic命令%pdb停止出错。

Seeing as you are comming from Matlab, I would suggest you give a look at 看到你是从Matlab来的,我建议你看看

Python(x,y) 蟒(X,Y)

The page decribes it as follows: 该页面描述如下:

Python(x,y) is a free scientific and engineering development software for numerical computations, data analysis and data visualization based on Python programming language, Qt graphical user interfaces and Spyder interactive scientific development environment. Python(x,y)是一个免费的科学和工程开发软件,用于基于Python编程语言,Qt图形用户界面和Spyder交互式科学开发环境的数值计算,数据分析和数据可视化。

It will not cater to all your wishes, but it certainly made me feel comfortable when I started out with Python, coming from Matlab. 它不会满足你的所有愿望,但当我开始使用Python时,它确实让我感到很舒服,来自Matlab。

Hope it helps 希望能帮助到你

If using the command line,如果使用命令行,

alias ipythondebug='ipython --InteractiveShell.pdb true'

in your ~/.profile will give you debug on error like Matlab.在你的 ~/.profile 中会给你像 Matlab 这样的错误调试。 This of course requires ipython installed.这当然需要安装 ipython。

Not sure about the resuming part.不确定恢复部分。

You can also edit the ipython config file if you want the debug on error to be permanent.如果您希望错误时的调试是永久性的,您还可以编辑 ipython 配置文件。 See https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-pdbhttps://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-pdb

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Python 在命令行中创建交互式应用程序,而不仅仅是返回 output 的脚本? - How do I make an interactive application at the command line with Python, rather than just a script which returns output? 如何在Win32上的Emacs中运行交互式命令行Python应用程序? - How do I run an interactive command line Python app inside of Emacs on Win32? 如何在调试模式下将命令行参数从 VS 传递给 Python? - How do I pass command line arguments to Python from VS in Debug mode? 如何调试内置的 Python 命令、包或模块? - How do I debug a built-in Python command, package or module? 在Python中,如何以交互方式提供命令行参数 - In Python, how to supply the command line arguments in interactive mode 如何在 Visual Studio Code 中查找/执行 Python 交互模式? - How do I find/excute Python Interactive Mode in Visual Studio Code? 如何使用python的交互式输入运行命令行程序? - How to run a command line program with interactive inputs from python? 如何在执行代码时打开或关闭Visual Studio python(Jupyter)交互式窗口(2019)? - How do i turn on or off the Visual Studio python (Jupyter) interactive window (2019) when executing code? 我如何像在spyder中一样在visual studio中激活交互式python帮助 - how do I activate interactive python help in visual studio like in spyder 如何检索上一个命令的输出并将其保存在Python交互式shell中的变量中? - How do I retrieve the output of a previous command and save it in a variable inside the Python interactive shell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM