search
HomeBackend DevelopmentPython TutorialSummary of usage of python debugging tool pdb (Python Debugger)

This article brings you relevant knowledge about Python, which mainly introduces the relevant content about the pdb debugging tool, including basic pdb commands, setting breakpoints with break, etc., Let’s take a look at it together, I hope it will be helpful to everyone.

Summary of usage of python debugging tool pdb (Python Debugger)

[Related recommendations: Python3 video tutorial ]

1. There are two ways to use pdb

pdb: python debugger

1. Non-intrusive method (No need to modify the source code, you can debug it by running it directly from the command line)

python3 -m pdb filename.py

2. Intrusive method (You need to add the following code to the code being debugged and then run the code normally)

import pdb pdb.set_trace()

When you see the following prompt on the command line, it means that pdb has been opened correctly

(Pdb)

2. pdb basic commands

##

In actual use, we found that when running a python file with a shell script, it may not be possible to debug using pdb and will exit. At this time, you can only run the py file directly for debugging.

3. Use the break command to set a breakpoint at the specified location in the specified file

3.1 Set a breakpoint at the specified location in this file

For example, in the following example, if you want to enter the forward() method of the model to view the data processing process during forward propagation, you can only set a break in the first line of forward() (line 26). Point, pdb.set_trace()

But sometimes the model is very complex, and using this method will cause the program to report an error and exit directly (I don’t know the reason), then we can consider using the break command here Insert a breakpoint in one line so that the program will stop when it reaches forward().

import torchimport torch.nn as nnimport pdbclass EncoderLayer(nn.Module):    def __init__(self):        super().__init__()
        self.conv1 = nn.Conv2d(4, 10, (3, 3))
        self.conv2 = nn.Conv2d(10, 4, (3, 3))
        self.relu = nn.ReLU()    def forward(self, x):
        x=self.relu(self.conv1(x))        return self.relu(self.conv2(x))class Encoder(nn.Module):    def __init__(self,num_layers):        super().__init__()        # encoders 由 num_layers个 EncoderLayer子层组成,每个子层结构相同,但参数不一定相同。
        self.ModelList = nn.ModuleList([EncoderLayer() for _ in range(num_layers)])    def forward(self, x):        # ModuleList是一个list,只能通过list的操作方式(如用for循环、下标索引等)进行forward计算。
        for layer in self.ModelList:
            x = layer(x)        return xif __name__=="__main__":
    pdb.set_trace()   
    input = torch.rand(5, 4, 30, 30)
    model = Encoder(num_layers=4)
    output = model(input)

Specific method: (1) First set pdb.set_trace() on any previous line to stop the program. (2) Just enter break 26. As shown in the picture:

Summary of usage of python debugging tool pdb (Python Debugger)In this way, the breakpoint is set successfully, and the program will stop when it reaches forward().

The 26 here is the number of lines. It should be noted that the breakpoint position cannot be a comment . For example, if we set a breakpoint on line 25 (comment line), it will fail:

Summary of usage of python debugging tool pdb (Python Debugger)To summarize, the command to set a breakpoint in the same file is:

break line

3.2 In others Set a breakpoint at a specified location in the file

If the breakpoint you want to set is not in the initial run file, how can you use the break command to set a breakpoint in other files? Let’s look at this example:

Divide the 3.1 code into three py files and put them under the same path:

  ![Summary of usage of python debugging tool pdb (Python Debugger)](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/4b5d476ba5b14b0ba541d78930b9704a~tplv-k3u1fbpfcp-zoom-1.image)

Take a look at the contents of each file:

run.py:

The initial pdb.set_trace() setting is in run.py.

import torchfrom encoder import Encoderimport pdbif __name__=="__main__":
    pdb.set_trace()    input = torch.rand(5, 4, 30, 30)
    model = Encoder(num_layers=4)
    output = model(input)

encoder.py:

from encoder_layer import EncoderLayerimport torch.nn as nnclass Encoder(nn.Module):    def __init__(self,num_layers):        super().__init__()        # encoders 由 num_layers个 EncoderLayer子层组成,每个子层结构相同,但参数不一定相同。
        self.ModelList = nn.ModuleList([EncoderLayer() for _ in range(num_layers)])    def forward(self, x):        # ModuleList是一个list,只能通过list的操作方式(如用for循环、下标索引等)进行forward计算。
        for layer in self.ModelList:
            x = layer(x)        return x

encoder_layer.py:

import torch.nn as nnclass EncoderLayer(nn.Module):    def __init__(self):        super().__init__()
        self.conv1 = nn.Conv2d(4, 10, (3, 3))
        self.conv2 = nn.Conv2d(10, 4, (3, 3))
        self.relu = nn.ReLU()    def forward(self, x):
        x=self.relu(self.conv1(x))        return self.relu(self.conv2(x))

Now we run run.py and then Set a breakpoint on line 12 of encoder.py, that is,

for layer in self.ModelList:

The command is:

break encoder.py:12

That is, break filename: lineSummary of usage of python debugging tool pdb (Python Debugger)We can see that the program can enter forward() from output = model(input):Summary of usage of python debugging tool pdb (Python Debugger)This is very convenient to debug.

If the initial breakpoint and the target breakpoint are not in the file in the same directory, you can also set the breakpoint through the file name under the relative path, such as:

(Pdb) break ../transformer/asr_model.py:91Breakpoint 1 at /local/wenet/examples/aishell/s0/wenet/transformer/asr_model.py:91(Pdb)

4. When using pdb Problems discovered

As shown in the figure, pdb It consists of three lines. The first line is the file path, the second line is the currently executed code line, and the third line is the input command line.

When there is a soft link, the path displayed by pdb is the path pointed by the soft link, but the actual code path is the path that copies the content of the soft link. These two paths are different, so you must pay attention. Summary of usage of python debugging tool pdb (Python Debugger)

4.2 pdb sometimes cannot add breakpoints in the forward() method of the model

pdbSometimes pdb cannot be used. set_trace() adds a breakpoint to the forward() method of the model. The error message is:

Compiled functions can't take variable number of arguments or use keyword-only arguments with defaul

Probably means "The compiled function cannot accept a variable number of parameters, nor can it use keyword-only parameters in default."

I don't understand what it means, and this problem has not been solved.

5. Post-debugging after the program crashes: pdb.pm()

As mentioned above, breakpoints are inserted when the program starts running, and pdb is used for debugging, that is, Predebug. In fact, pdb can also be used for post-mortem debugging, that is, after the program has a bug and crashes, use the python debugger to view it.

For example, test.py is obviously buggy:

# test.pydef add(n):    return n+1add("hello")

Run directly:

python test.py

The program runs Collapse:

F:\PycharmProjects\pytorch_practice>python test.py
Traceback (most recent call last):
  File "test.py", line 4, in <module>
    add("hello")
  File "test.py", line 2, in add
    return n+1
TypeError: can only concatenate str (not "int") to str</module>

In this way we cannot use pdb for debugging. So when the program crashes, how should we debug it?

We can use the following command for simple debugging:

python -i test.py

-i option allows you to open a Interactive shell, as follows:

F:\PycharmProjects\pytorch_practice>python -i test.py
Traceback (most recent call last):
  File "test.py", line 4, in <module>
    add("hello")
  File "test.py", line 2, in add
    return n+1
TypeError: can only concatenate str (not "int") to str
>>></module>

现在我们发现程序结束后出现了 >>> 符号,这就是python调试器。

输入命令:

import pdb pdb.pm()

其中 pdb.pm() 用于程序发生异常导致奔溃后的事后调试,可以跟踪异常程序最后的堆在信息。

执行命令后得到:

TypeError: can only concatenate str (not "int") to str
>>> import pdb
>>> pdb.pm()
> f:\pycharmprojects\pytorch_practice\test.py(2)add()
-> return n+1
(Pdb)

可以发现,pdb.pm() 已经追踪到了导致程序奔溃的语句:return n+1

此时可以打印 n 的值进行检查:

(Pdb) p n'hello'(Pdb) q>>> quit()

F:\PycharmProjects\pytorch_practice>

q 表示退出pdb调试,quit() 表示退出 python 调试器。

【相关推荐:Python3视频教程

Command Explanation
break or b Set breakpoint
continue or c Continue executing the program
list or l View the code segment of the current line
step or s Enter the function (use next instead of step to enter the for loop)
return or r Execute code until returning from the current function
next or n Execute the next line
up or u Return to the previous call point (not the previous line)
p x Print variables The value of

The above is the detailed content of Summary of usage of python debugging tool pdb (Python Debugger). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:掘金. If there is any infringement, please contact admin@php.cn delete
Python and Time: Making the Most of Your Study TimePython and Time: Making the Most of Your Study TimeApr 14, 2025 am 12:02 AM

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python: Games, GUIs, and MorePython: Games, GUIs, and MoreApr 13, 2025 am 12:14 AM

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python vs. C  : Applications and Use Cases ComparedPython vs. C : Applications and Use Cases ComparedApr 12, 2025 am 12:01 AM

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

The 2-Hour Python Plan: A Realistic ApproachThe 2-Hour Python Plan: A Realistic ApproachApr 11, 2025 am 12:04 AM

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python: Exploring Its Primary ApplicationsPython: Exploring Its Primary ApplicationsApr 10, 2025 am 09:41 AM

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

How Much Python Can You Learn in 2 Hours?How Much Python Can You Learn in 2 Hours?Apr 09, 2025 pm 04:33 PM

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

How to teach computer novice programming basics in project and problem-driven methods within 10 hours?How to teach computer novice programming basics in project and problem-driven methods within 10 hours?Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading?How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading?Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool