preface

PyCharm is the easiest way to debug Python programs remotely, but PDB is the easiest way to do it. In this article, you can learn how to use PyCharm and how to use PDB.

To learn a debugging tool, you can start with the four most critical features:

  • 1. How to set breakpoints (Add and delete breakpoints)
  • 2. How to execute breakpoint code
  • 3. How do I view data in the memory
  • 4. How do I operate data in memory

PDB is a built-in debugging tool for Python. You don’t need to install it.

1. How do I set a breakpoint

Prepare a simple piece of code, such as one that computes the Fibonacci sequence. .

def Fibonacci(n):
    a, b = 0.1
    while n > 0:
        a, b = b, a + b
        print(b)
        n -= 1

Fibonacci(10)
Copy the code

Set breakpoints there are two kinds of breakpoints, one is intrusive add breakpoints, you need to add ** “import PDB;pdb.set_trace()” to the breakpoint, the other is non-intrusive add breakpoints, using -m to specify the project to run through the PDB. The complete command is “python3 -m PDB Fibonacci sequence. Py” **, which will type the breakpoint at the entry of the program.

You can then use the ** “l” ** command to view the 11 lines of project code around the breakpoint. The location of the breakpoint is indicated by the “->” symbol, as shown below.

(Pdb) l 1 def Fibonacci(n): 2 a, b = 0, 1 3 while n > 0: 4 import pdb; pdb.set_trace() 5 -> a, b = b, a + b 6 print(b) 7 n -= 1 8 9 Fibonacci(10) [EOF]Copy the code

If you use the “L” command multiple times, you can realize the page to view the code under the current breakpoint, if you need to view all the code of the current function or module, you can use the ** “ll” ** command.

Debug line code is usually debugged non-invasively and breakpoints are added with the ** “b” ** command, which can be used in several ways:

  • 1. Run the B command alone to view breakpoint Settings
  • 2. Run the “b lineno” command to add a breakpoint on lineno of the current module
  • 3. Using b filename:lineno will add a breakpoint on the lineno line of a file filename
  • 4. Using the “b function” command sets a breakpoint on the first line of a function

Just a little bit.

(Pdb) b 7 # Add a breakpoint at line 7
Breakpoint 1The at/Users/ayuliao/Desktop/workspace/test/the Fibonacci sequence. Py:7(Pdb) b Fibonacci sequence. Py:8 # Add a breakpoint at line 8 of the Fibonacci sequence. Py file
Breakpoint 2The at/Users/ayuliao/Desktop/workspace/test/the Fibonacci sequence. Py:8
(Pdb) b Fibonacci # Add a breakpoint at line 1 of the function named Fibonacci
Breakpoint 3The at/Users/ayuliao/Desktop/workspace/test/the Fibonacci sequence. Py:1
(Pdb) b Check the breakpoint status
Num Type         Disp Enb   Where
1Breakpoint keep yes at/Users/ayuliao/Desktop/workspace/test/the Fibonacci sequence. Py:7
2Breakpoint keep yes at/Users/ayuliao/Desktop/workspace/test/the Fibonacci sequence. Py:8
3Breakpoint keep yes at/Users/ayuliao/Desktop/workspace/test/the Fibonacci sequence. Py:1
(Pdb)
Copy the code

Besides can be “tbreak” * * * * command to add a temporary breakpoint, the so-called temporary breakpoint is used once, the breakpoint will be clear, it can be used in the loop body, a loop, if through the command “b” breaking point, then each cycle processing card at the command “b” set a breakpoint, if use “tbreak” command, Breakpoints are only valid once in the body of the loop.

The usage of the tbreak command is the same as that of the b command

tbreak
tbreak lineno
tbreak filename:lineno
tbreak functionname
Copy the code

To clear up a breakpoint, use the ** “cl” command **. This command can be used in the following ways:

  • 1. The “cl” command alone clears all breakpoints, including temporary breakpoints, and prompts for confirmation before cleaning
  • 2. Run cl filename:lineno to clear a breakpoint on a line in a file

Just a little bit.

(Pdb) b # check the breakpoint condition Num Type Disp Enb Where 1 breakpoint keep yes at/Users/ayuliao/Desktop/workspace/test/the Fibonacci sequence. Py: 1 2 Breakpoint keep yes at/Users/ayuliao/Desktop/workspace/test/the Fibonacci sequence. Py: 2, 3, breakpoint keep yes the at / Users/ayuliao/Desktop/workspace/test/the Fibonacci sequence. Py: 7 (Pdb) cl the Fibonacci sequence. Py: 7 # to delete the Fibonacci sequence. Py corresponding breakpoint (Pdb) file line 7 b # to check the current state breakpoint Num Type Disp Enb Where 1 breakpoint keep yes at/Users/ayuliao/Desktop/workspace/test/the Fibonacci sequence. Py: 1 2 breakpoint keep yes the at / Users/ayuliao/Desktop/workspace/test/the Fibonacci sequence. Py: 2 (Pdb) cl # Clear all breakpoints Clear all breaks? Y does breakpoint at 1 / Users/ayuliao/Desktop/workspace/test/the Fibonacci sequence. Py: 1 does breakpoint 2 ats / Users/ayuliao/Desktop/workspace/test/the Fibonacci sequence. Py: 2 (Pdb) b (Pdb)Copy the code

2. How to execute breakpoint code

PDB provides a variety of command implementation code execution, the overall can be divided into line – by – line command execution and non-line – by – line command execution.

** the “s”, “n”, and “r” commands are very similar in that they all execute code line by line, the main difference being how functions are treated.

  • 1. The “s” command executes the next line of code. If the next line is a function call, it enters the function body
  • 2. The “n” command executes the next line and does not enter the function body when a function is called
  • 3. The “r” command also executes the next line of code. When a function is called, it directly executes the last line of the function body

If you want to execute code non-line by line, use the ** “c” command or the “unt” command **.

  • 1. The “c” command will execute to the next breakpoint, or if the last breakpoint is broken, the entire program will be executed
  • 2. The “unt lineno” command is executed on a specified line or at the location of the next breakpoint.

In addition, it is possible to jump to a line with the ** “j” ** command, but this command skips the middle code and does not execute it.

3. How do I view data in the memory

The purpose of the break point at a certain location is to know whether the variable state of the program is normal until the current location, which requires some way to look at the data in memory.

The most common is to use ** “p” command to view the value of a variable in the current memory, similar to using the print() method to print a variable in code, if the print is a variable with a complex structure, you can use the “pp” command to beautify the printed result, if you need to know the type of the variable, You can view this by using the “whatis” ** command.

In addition, if the breakpoint is in the function body, you can view the parameters of the current function body and their corresponding values by using the ** “a” ** command.

Use these commands briefly.

<class 'int'> (Pdb) a # Fibonacci = Fibonacci (Pdb) pp b # View the value 1 of variable B (Pdb)Copy the code

4. How do I operate data in memory

PDB provides a variety of ways to manipulate the data in memory at the current breakpoint. The most common way is to use the ** “interact” ** command to enter the interactive interpreter and manipulate the data in memory.

Modifying data in memory in the interact state does not have a substantial impact on the program. All changes are cleared after the interact state exits.

In Windows, you can use CTRL + D to exit the interact state and return to the PDB. In MacOS, you need to use control+ D to exit the interact state.

Just a little bit.

(Pdb) interact Enter the interactive interpreter to manipulate data currently in memory
*interactive*
>>> a
0
>>> a = a + 1 Change the value of a variable in memory
>>> print(a)
1
>>> b
1
>>> ^D # control+ D exits the interact state
now exiting InteractiveConsole...
(Pdb) p a The value of variable A has not been changed
0
Copy the code

At the end

PDB is a simple tool that allows you to easily debug Python code without an IDE.

If the article is helpful, please click “watching” or “admiring”, this is the biggest encouragement to me.