简体   繁体   English

如何在没有交互式控制台的情况下进行调试

[英]How do you debug without an interactive console

As a self-taught programmer, I learned to debug using an interactive console that kept all of my variables in memory when I build /run the script. 作为一名自学成才的程序员,我学会了使用交互式控制台进行调试,该控制台在构建/运行脚本时将所有变量保存在内存中。 However, I noticed the overwhelming trend for debugging in IDEs (and, I suppose CLI + Editor solutions, for that matter) is to build your script in one place and provide a separate console "sandbox" type area that only keeps variables if you copy/paste your code. 但是,我注意到在IDE中进行调试的压倒性趋势(为此,我认为CLI +编辑器解决方案)是在一个地方构建脚本并提供单独的控制台“沙盒”类型区域,该区域仅在复制时保留变量/粘贴您的代码。

How do you debug without an interactive console? 在没有交互式控制台的情况下如何调试? Can anyone list a few debugging steps that could help me be a better programmer / debugger? 谁能列出一些调试步骤来帮助我成为更好的程序员/调试器?

Currently, this is a very simplified version of what I do: 目前,这是我所做工作的非常简化的版本:

  1. Write some pseudocode (sometimes) 编写一些伪代码(有时)
  2. Write some code in an editor that should work 应该工作的编辑器中编写一些代码
  3. run / build the script 运行/构建脚本
  4. Check stdout for errors 检查标准输出是否有错误
  5. If no errors, then 7. 如果没有错误,则为7。
  6. If errors, then back to 2 after fixing the offending code. 如果有错误,请在修复有问题的代码后返回2。
  7. Type variable names into console to verify that they look like I anticipated. 在控制台中键入变量名称,以验证它们是否像我预期的那样。
  8. Rinse and Repeat until it works as I intended. 冲洗并重复,直到达到我的预期效果。

The best way to do this would be to write tests . 最好的方法是编写测试 That automates steps 3 through 7 for you. 这样可以自动执行第3步到第7步。 It also prevents regressions from occurring when you change other code. 它还可以防止在更改其他代码时发生回归。

you can use the q module for this easily https://pypi.python.org/pypi/q 您可以轻松地使用q模块https://pypi.python.org/pypi/q

xyxy.py xyxy.py

import q
do_something()
q.d() #this will open interactive shell

def f():
    do_something()
    q.d() #open console here with access to all local variables of f

you can also use automated tests (builtin unittest module or nosetests or something else) 您还可以使用自动测试(内置的unittest模块或鼻子测试或其他工具)

使用一个不错的python IDE-那里有很多东西,您将可以通过在鼠标悬停或添加监视来在断点处停止检查变量,并进入一个上下文控制台,在该控制台中您可以在断点的上下文中与代码进行交互。

在问题代码区域之间使用打印语句...否则,只需下载一个好的IDE

It turns out that PyCharm, at least, DOES have an interactive console and the default keymapping (on Mac) is option-shift-E. 事实证明,PyCharm至少有一个交互式控制台,并且默认的键盘映射(在Mac上是)是option-shift-E。 Then your variables are loaded in memory. 然后将变量加载到内存中。 However, the suggestions above are better programming practices. 但是,以上建议是更好的编程实践。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM