简体   繁体   English

在Python中使用控制台中的断点进行调试

[英]Debugging with breakpoints from console in Python

I'm trying to migrate from Matlab to python. 我正在尝试从Matlab迁移到python。 One of the things that is nice about Matlab is that when debugging I can put a breakpoint in some code and do something to call that code form the command line. 关于Matlab的一个好处是,在调试时我可以在某些代码中放置断点并执行某些操作从命令行调用该代码。 Using PyCharm + IPython I haven't found a way to do this in Python. 使用PyCharm + IPython我还没有找到在Python中执行此操作的方法。 It seems I have to run an entire script in debug mode to do any debugging rather than being able to do so from a simple command. 我似乎必须在调试模式下运行整个脚本来执行任何调试,而不是通过简单的命令来执行此操作。 I suppose I could write a one line script with the command I'm interested in, but it seems like there should be a better way. 我想我可以用我感兴趣的命令编写一行脚本,但似乎应该有更好的方法。 What is the Python way of doing this? 这样做的Python方法是什么?

I would like to recommend using Python Tools for Visual Studio . 我想建议使用Visual Studio的Python工具 It is free and open source, and although Visual Studio itself is obviously not open source, there is ( "PTVS Integrated" ) which comes with a free and very functional version of Visual Studio with commercial use permitted. 它是免费和开源的,虽然Visual Studio本身显然不是开源的,但是( “PTVS集成” )附带一个免费且功能非常强大的Visual Studio版本,允许商业使用。 Additionally, students and staff of most academic institutions will have free access to Visual Studio Ultimate via Dreamspark . 此外,大多数学术机构的学生和工作人员都可以通过Dreamspark免费访问Visual Studio Ultimate。

If your program is stopped at a breakpoint, you can open "Python Debug Interactive" (from tools->python tools), which will open an interactive python shell with access to all of the variables available in your program namespace at the breakpoint, in the same way that you can do in Matlab. 如果程序在断点处停止,则可以打开“Python Debug Interactive”(来自tools-> python工具),这将打开一个交互式python shell,可以访问断点处程序命名空间中可用的所有变量,就像在Matlab中一样。

Hovering over the variables with your mouse in the source code also shows the value, bringing up the "locals" window more or less simulates the workspace viewer in Matlab, and you can also "watch" specific variables. 将鼠标悬停在源代码中的变量上也会显示该值,显示“locals”窗口或多或少会模拟Matlab中的工作区查看器,您还可以“监视”特定变量。 I don't know if it's safe to edit the variables through this interface though, use with caution! 我不知道通过这个界面编辑变量是否安全,请谨慎使用!

Unfortunately PTVS doesn't have nested breakpoints, which is a quite useful feature in the Matlab debugger. 不幸的是,PTVS没有嵌套断点,这是Matlab调试器中非常有用的功能。 So if you are stopped at a breakpoint and you call a method from the debug interactive window, any breakpoints in the method will not work. 因此,如果您在断点处停止并从调试交互窗口调用方法,则方法中的任何断点都将无效。 See this related question . 看到这个相关的问题

The arrow key based command history in the debug shell is quite primitive compared to Matlab or ipython, and the Intellisense is not as good as it is for native .net languages, but I've been using it solidly for the last half-year or so now, and don't really feel like I'm missing much from Matlab, other than the excellent documentation. 与Matlab或ipython相比,调试shell中基于箭头键的命令历史是非常原始的,并且Intellisense不如原生.net语言那么好,但我在过去的半年里一直坚持使用它或者所以现在,除了优秀的文档之外,我并不觉得我在Matlab上错过了很多东西。

One other thing to be aware of is that the code execution performance when in debug mode is MUCH slower, so I recommend either running your code without debug mode (using "Ctrl+F5" instead of "F5") for best performance, or the new mixed mode debugger if you need both breakpoints and good performance. 另一件需要注意的事情是,在调试模式下代码执行性能要慢得多,所以我建议在没有调试模式的情况下运行代码(使用“Ctrl + F5”代替“F5”)以获得最佳性能,或者新的混合模式调试器,如果您需要断点和良好的性能。

Try using python debugger 尝试使用python调试器

b(reak) [[filename:]lineno | function[, condition]]

or 要么

pdb.set_trace();

More detailed tutorial can be found here . 更详细的教程可以在这里找到

Have you tried Spyder??? 你试过Spyder ??? This is an open source IDE that looks very similar to Matlab. 这是一个与Matlab非常相似的开源IDE。 It also provides the debugger that you want. 它还提供了您需要的调试器。 https://code.google.com/p/spyderlib/ https://code.google.com/p/spyderlib/

PS: I'm also migrating to python, but I'm avoiding things like that because I want to start with an empty mind. PS:我也正在迁移到python,但我正在避免这样的事情,因为我想从一个空洞的头脑开始。 :) But I read lots of those from Matlab to Numpy texts... :)但我读了很多从Matlab到Numpy文本的内容......

I have been moved from matlab and R to python. 我已经从matlab和R转移到了python。 I have tried different editors so I can give you some advices. 我尝试了不同的编辑器,所以我可以给你一些建议。

1- Spyder is the closer to matlab but my impression is that it is not very good. 1- Spyder更接近matlab,但我的印象是它不是很好。 It often crash when I start to run long simulation with a lot of data. 当我开始使用大量数据进行长时间模拟时,它经常会崩溃。 If you are new to python I suggest you to use this one for a while and then move to something else. 如果你是python的新手我建议你暂时使用这个,然后转移到其他东西。

2- emacs python mode. 2- emacs python模式。 Works very well . 效果很好。 In my opinion it is difficult to configure and probably not the best choice if you are not familiar with python. 在我看来,如果你不熟悉python,很难配置,可能不是最好的选择。

3- pycharm. 3- pycharm。 I have just started to use pycharm and it seems to be very good (this reminds my Rstudio). 我刚开始使用pycharm,它看起来非常好(这让我想起了Rstudio)。 I do not think that this supports an interactive console like the one inside spyder or emacs. 我认为这不支持像spyder或emacs这样的交互式控制台。 You can still obtain something similar in the debug mode 您仍然可以在调试模式下获得类似的东西

4- A lot of people love ipython notebook but I think that this is not a good choice for long code. 4-很多人都喜欢ipython笔记本,但我认为这不是长代码的好选择。 It is good if you want something easy to visualize. 如果你想要一些易于想象的东西,这是很好的。

Since you mentioned you are using ipython , you can also check ipdb . 既然你提到你正在使用ipython ,你也可以检查ipdb

You have to install it first via pip or easy_install . 您必须先通过pipeasy_install安装它。 Etc: 等等:

pip install ipdb

The usage is the same as pdb. 用法与pdb相同。 The ipython console will popout where you placed ipdb.set_trace() from where you can check/change local an global variables, check their documentation and types, step into the code of incoming functions (with 's' you'll go to the definition of code123() ), etc. ipython控制台将弹出你放置ipdb.set_trace()的位置,你可以从那里检查/更改本地的全局变量,检查它们的文档和类型,进入传入函数的代码(用's'你会去定义code123() )等

import ipdb;

code000()
ipdb.set_trace();
code123()

Also a tip on how to get the functionalities of ? 还有关于如何获得功能的提示? from ipython (regarding getting the documentation of functions and modules while in the debugger). 来自ipython(关于在调试器中获取函数和模块的文档)。 This answer . 这个答案

In console create function where you use pdb.set_trace() , then function you want debug. 使用pdb.set_trace()的控制台创建函数中,然后是想要调试的函数。

>>> import pdb

>>> def f():
...     pdb.set_trace()
...     my_function()
... 

Then call created function: 然后调用创建的函数:

>>> f()
> <stdin>(3)f()
(Pdb) s
--Call--
> <stdin>(1)my_function()
(Pdb) 

Happy debugging :) 快乐调试:)

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

相关问题 PyDev:如何从控制台调用特定于调试的命令(带断点)? - PyDev: How to invoke debugging specific command from console (with breakpoints)? 调试Python Flask应用程序时,VS代码中的断点未达到 - Breakpoints are not hitting in VS Code while debugging Python Flask app 在 Python 中的 VSCode 中调试,调试器不遵守 class 中的断点 - Debugging in VSCode in Python, debugger does not respect the breakpoints in class 从Python Breakpoints包设置的断点不起作用 - Breakpoints set from Python Breakpoints packages didn't work Qt调试崩溃/退出python,最终无法正确调试(例如,未在断点处停止) - Qt debugging crashes/quits python and end up not debugging properly (e.g. not stopping at breakpoints) 使用python.pythonPath时,VSCode Python调试不遵循断点 - VSCode Python Debugging Doesn't Honor Breakpoints When Using python.pythonPath 为什么 VSCode 不在断点处停止调试? - Why is VSCode not stopping at breakpoints for debugging? 在功能范围内交互使用python控制台而无需调试模式 - interactive use of python console inside function scope without debugging mode 使用Python调试Windows控制台应用程序的输出读取 - Debugging the reading of output of a Windows console app using Python 如何在程序中启动 python 控制台(以便于调试)? - How can I start the python console within a program (for easy debugging)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM