手机怎么安装py thon

Python pdb module provides an interactive debugging environment for Developers to debug Python programs. Today we will explore python debugger examples using pdb module and look at the various ways of debugging a python program.

Python pdb模块为开发人员提供调试Python程序的交互式调试环境。 今天,我们将探讨使用pdb模块的python调试器示例,并介绍各种调试python程序的方法。

Python调试器– Py​​thon PDB (Python Debugger – Python PDB)

The pdb module in Python provides us with immense features for effective debugging of python code. This includes:

Python中的pdb模块为我们提供了巨大的功能,可以有效地调试python代码。 这包括:

  • Pause a program暂停程序
  • Look at the values of variables看一下变量的值
  • Look at program execution on every line查看每一行的程序执行

These are just some points that make pdb module a boon for Python programmers. Let’s look at how we can start using this module.

这些只是使pdb模块成为Python程序员的福音的几点。 让我们看看如何开始使用此模块。

启动Python调试器 (Starting Python Debugger)

Well, it isn’t about the program. We can start pdb with any program. Here is a sample program:

好吧,这与程序无关。 我们可以使用任何程序启动pdb。 这是一个示例程序:

class User:def __init__(self, runner):self.counter = runnerdef showSomething(self):for i in range(self.counter):print(i)returnuser = User(5)
user.showSomething()

Let’s see the output for this program:

让我们看一下该程序的输出:

We can go to next line in pdb execution by pressing ‘n’ followed by Enter key.

我们可以通过按“ n”,然后按Enter键转到pdb执行的下一行。

在程序中启动pdb (Starting pdb within the program)

Above example started pdb from the command line, so, pdb tracing started from the very first line of execution. Usually, we only want to debug a specific area of a program which comes much after the program has started.

上面的示例从命令行启动了pdb ,因此pdb跟踪从执行的第一行开始。 通常,我们只想调试程序的特定区域,该区域是在程序启动后很长时间才出现的。

To achieve this, we use pdb in our programs itself. Let’s look at a code snippet:

为此,我们在程序本身中使用了pdb。 让我们看一下代码片段:

import pdbclass User:def __init__(self, runner):self.counter = runnerdef showSomething(self):for i in range(self.counter):pdb.set_trace()print(i)returnuser = User(5)
user.showSomething()

Let’s see the output for this program:

Press n and Enter to go to next line and watch the execution.

让我们看一下该程序的输出:

按n和Enter键转到下一行,观察执行情况。

事后调试 (Post-mortem debugging)

Once the program is finished with the execution process, debugging a failure in it is termed as post-mortem debugging. The module supports this as well with few more functions:

一旦程序完成了执行过程,将其中的故障调试称为事后调试。 该模块还支持以下功能:

import pdbclass User:def __init__(self, runner):self.counter = runnerdef showSomething(self):for i in range(self.runner):pdb.set_trace()print(i)returnuser = User(5)
user.showSomething()

Here, we have mentioned self.runner which doesn’t exist. We can try these functions inside Python interpreter itself:

在这里,我们提到了不存在的self.runner 。 我们可以在Python解释器内部尝试以下功能 :

检查堆栈上的变量 (Checking variables on the Stack)

One of the points of using a debugger at all is that we should be able to check the program stack and what variables are stored on that stack during program execution. We can do this with pdb as well. Let’s look at a code snippet:

完全使用调试器的要点之一是,我们应该能够检查程序堆栈以及在程序执行期间在堆栈上存储了哪些变量。 我们也可以使用pdb做到这一点。 让我们看一下代码片段:

import pdbdef recursive_calls(i = 5, output = 'somethign here'):if i > 0:recursive_calls(i - 1)else:pdb.set_trace()print(output)returnif __name__ == '__main__':recursive_calls()

This is a simple example for a recursion. Let’s see the output for this program:

这是一个简单的递归示例。 让我们看一下该程序的输出:

Note that we used the where command to print variables on stack. We can also print a specific variable as:

请注意,我们使用where命令在堆栈上打印变量。 我们还可以将特定变量打印为:

For variables which might contain large values like file data etc., we also pretty print the values. So, if the variables is data, we can run the command:

对于可能包含较大值(例如文件数据等)的变量,我们也可以漂亮地打印这些值。 因此,如果变量是data ,我们可以运行以下命令:

pp data

Python pdb断点 (Python pdb Breakpoint)

Using n to go to next line is useful but what if you actually know where to stop? Also, debugging the whole program and moving through each line is tedious when you know where you want to actually stop!

使用n转到下一行很有用,但是如果您实际上知道在哪里停下来怎么办? 另外,当您知道要实际停止的位置时,调试整个程序并在每一行中移动都是很麻烦的!

With pdb, we can also use breakpoints. For this, we need to inform pdb which line to set a breakpoint to. Here is a sample program and demonstration:

使用pdb,我们还可以使用断点。 为此,我们需要通知pdb将断点设置pdb一行。 这是一个示例程序和演示:

import pdbdef recursive_calls(i = 5, output = 'somethign here'):if i > 0:recursive_calls(i - 1)else:print(output)returnif __name__ == '__main__':recursive_calls()

Nothing unusual here actually. Let’s see the demonstration with this program:

We used the break keyword along with the line number where we want to set the breakpoint.

实际上没有什么异常。 让我们看一下该程序的演示:

我们将break关键字与要设置断点的行号一起使用。

管理断点 (Managing Breakpoints)

As we saw in the last demonstration, when we applied a breakpoint to a line, it was assigned an identifier as Breakpoint 1. The numeric part can be used as an ID to enable and disable these breakpoints interactively. Disabling a breakpoint with disable keyword informs the debugger not to halt execution when that line is reached. The breakpoint is still stored but ignored.

正如在上一个演示中所看到的,当我们将断点应用于行时,它被分配了一个标识符作为Breakpoint 1 。 数字部分可以用作ID,以交互方式启用和禁用这些断点。 使用disable关键字禁用断点会通知调试器在到达该行时不要停止执行。 断点仍被存储但被忽略。

Let’s see how this is done:

让我们看看如何做到这一点:

结论 (Conclusion)

In this post on python debugging, we saw how we can effectively debug a Python program and identify any issues we face using pdb module.

在有关python调试的这篇文章中,我们看到了如何有效地调试Python程序并使用pdb模块确定遇到的任何问题。

Reference: API Doc

参考: API文档

翻译自: https://www.journaldev.com/19114/python-debugger-python-pdb

手机怎么安装py thon

手机怎么安装py thon_Python调试器– Py​​thon pdb相关推荐

  1. 试图运行项目时出错,无法启动调试。没有正确安装调试器,请运行安装程序安装或恢复调试器。...

            用Visual Studio.net 2003调试项目时,出现错误对话框,显示如下:         试图运行项目时出错,无法启动调试.没有正确安装调试器,请运行安装程序安装或恢复调试 ...

  2. Visual Studio2008安装后更换调试器的问题

    在电脑上同时装了Visual Studio2008和VC++6.0.用VC++6.0写程序出错时,系统会启动Visual Studio2008的调试器.本来也没什么,但我写的都是一些小程序,不至于调用 ...

  3. zend stuido 12.5的插件安装和xdebug调试器的配置和和配置注意

    参考: zend stuido 12.5的插件安装 zend 12.5 安装插件是按类别进行分类了的, 而且是在欢迎 界面就可以直接安装, 安装后,要重启zend才能生效 版式设计的一个基本点就是: ...

  4. Window 7下QT5.9.2安装、QTCreator调试器配置

    QT 5.9.2下载 QT在5.9版本及以上将不同的编译器的版本放在同一个文件qt-opensource-windows-x86-5.9.x.exe中,所以只需要此文件即可,链接:qt-opensou ...

  5. ubuntu安装go语言调试器dlv

    简言 1. 网上ubuntu安装dlv的教程大多出自同一篇博客,使用的版本比较老,需要修改go.mod才能编译,略麻烦 2. 其实新版本的dlv已经修复这个问题,下载后直接编译即可成功 3. 这篇博客 ...

  6. 试图运行项目时出错,无法启动调试。没有正确安装调试器--很多次都是上网找了很多资料,都很难解决

    试图运行项目时出错,无法启动调试.没有正确安装调试器.请运行安装程序安装或修复调试器 收藏 对于这样的问题,出现很多次,很多次都是上网找了很多资料,都很难解决,只好重装IIS或者是.NET. 通过网上 ...

  7. Python 调试器入门

    Python 生态系统包含丰富的工具和库,可以让开发人员更加舒适. 例如,我们之前已经介绍了如何使用交互式 shell 增强 Python.本文重点介绍另一种可以节省时间并提高 Python 技能的工 ...

  8. 【Python基础】Python调试器pdb

    Python调试器pdb 1. pdb简介 2. pdb调试 2.1 pdb常用命令 2.2 pdb实例 更新历史: 2022年12月6日完成初稿 最近在写项目代码,其中需要在Vscode上写pyth ...

  9. Qt Creator设置调试器

    Qt Creator设置调试器 设置调试器 支持的本机调试器版本 支持的GDB版本 支持的CDB版本 支持的LLDB版本 安装本机调试器 GDB Windows调试工具 适用于macOS的调试工具 本 ...

最新文章

  1. deeplearning量化
  2. 求生之路怎么显示服务器,求生之路怎么搭建云服务器
  3. NGINX 配置404错误页面转向
  4. 项目总是有红叉叉,打开又没错,Build path contains duplicate entry: '...' for project 'X
  5. 编码的喜悦……以及Java中的变异测试
  6. 进程控制:进程的创建、终止、阻塞、唤醒和切换
  7. java -jar 默认参数_JAVA入门学习指南,建议收藏
  8. 运行Python时出现SyntaxError: EOL while scanning string literal解决方法
  9. day14 Python函数
  10. [转]微软SerialPort秘籍[SerialPort为什么死锁程序的分析]
  11. android 5.0 屏幕录制,Android 5.0+ 视频录制 ScreenCapture
  12. linux win10五笔码表,wubiLex(Win10微软五笔码表安装管理助手)V9.6.0.1 正式版
  13. ARM嵌入式系统C语言编程分析
  14. 解决Unresolved external ‘AlphaBlend‘ referenced的办法
  15. AI大神各显神通!百度深度学习集训营作品大赏
  16. 锁定计算机屏幕的快捷键是什么,锁住电脑屏幕的快捷键_打开电脑屏幕的快捷键...
  17. 为RemoteApp的登录用户(域用户)添加输入法的方法
  18. Ubuntu 如何安装 微信?实测
  19. 茜茜:大二开始布局学习大数据,结果如何?
  20. MATLAB 区分大小写问题

热门文章

  1. XCL-Charts圈图
  2. (转)Mahout Kmeans Clustering 学习
  3. windows 2003内存设置
  4. javascript权威指南 学习笔记之变量作用域
  5. 公司新办公楼休息间能看到富士山了
  6. [转载] python字符串分割
  7. SQL Server实际执行计划COST欺骗案例
  8. 【译】Input Method Manager (IMM):输入法管理器
  9. SharePoint:扩展DVWP - 第34部分:使用图标形式的表单操作链接
  10. JS与Asp.Net的传值