python debug Utility pdb Explanation
- 2021-07-16 02:41:07
- OfStack
To nag and nag
First of all, we introduce pdb debugging under 1. pdb is a built-in module of python, which is used to debug Python code from the command line. You might say that it is very convenient to debug code with editors such as Pycharm now, so why use the command line? I once thought about this problem, until once, the code had to run on Linux system (now Pycharm can also debug the code remotely, so let's not talk about this today)
Introduction to use
How do I add breakpoints?
When it comes to debug, it is definitely necessary to add breakpoints. There are two ways to add breakpoints:
Add 1 line after you want the breakpoint code
pdb.set_trace()
If you use this method, you can enter breakpoint debugging by running Python file directly.
Use the command line to add breakpoints
b line_number (Number of lines of code)
If you use this method, you need python-m pdb xxx. py to start breakpoint debugging.
Common commands
First of all, a brief introduction to the use of 1 command, don't remember here, and when you use it, just come back and check it.
1 Enter command line Debug mode,
python -m pdb xxx.py
2 h: (help) Help
3 w: (where) Print the current execution stack
4 d: (down) Performs a jump to the deep layer 1 of the current stack (I don't think it's useful)
5 u: (up) Performs a jump to the top 1 layer of the current stack
6 b: (break) Add Breakpoints
b lists all current breakpoints and the number of times breakpoints have been executed to statistics b line_no: Adds a breakpoint on line line_no of the current script b filename: line_no: Script filename Line line_no Add Breakpoint b function: Add a breakpoint at the first executable statement of function function7 tbreak: (temporary break) Temporary Breakpoint
After the first execution to this breakpoint, this breakpoint is automatically deleted, which is similar to b1
8 cl: (clear) Clear Breakpoint
cl Clear All Breakpoints cl bpnumber1 bpnumber2... Clear breakpoints with breakpoint numbers bpnumber1, bpnumber2... cl lineno clears breakpoints in the lineno line of the current script cl filename: line_no clears breakpoints in the line_no line of the script filename9 disable: Disable breakpoint with parameter bpnumber, the difference with cl is that the breakpoint still exists, but it is not enabled
10 enable: Activate breakpoint with parameter bpnumber
11 s: (step) Execute the next command
If this sentence is a function call, s executes to the first sentence of the function
12 n: (next) Executes the next statement
If this sentence is a function call, the function is executed, followed by the next one of the currently executed statements.
13 r: (return) Executes the currently running function to the end
14 c: (continue) Continue until the next breakpoint is encountered
15 l: (list) List source code
l lists 11 pieces of code around the current execution statement l first Lists 11 codes around line first l first second Lists codes in the first--second range, if second < first, second will be resolved to the number of rows16 a: (args) Lists the functions that are currently executing the function
17 p expression: (print) Output the value of expression
18 pp expression: p expression
19 run: Restart debug, equivalent to restart
20 q: (quit) Exit debug
21 j lineno: (jump) Sets the following statement function to execute
You can only jump at the bottom of the stack, re-execute backward, and execute directly to the line number forward
22) unt: (until) Executes to the next line (jumps out of the loop), or the current stack ends
23) condition bpnumber conditon, setting a condition for the breakpoint. When the parameter condition returns True, the bpnumber breakpoint is valid, otherwise the bpnumber breakpoint is invalid
Take a simple chestnut
To verify the usage of pdb under 1, I wrote a simple Python code, as follows:
__author__ = 'zone'
__gzh__ = ' Official number: zone7'
import pdb
class MyScrapy:
urls = []
def start_url(self, urls):
pdb.set_trace()
for url in urls:
print(url)
self.urls.append(url)
def parse(self):
pdb.set_trace()
for url in self.urls:
result = self.request_something(url)
def request_something(self, url):
print('requesting...')
data = '''<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
</body>
</html>'''
return data
scrapy= MyScrapy()
scrapy.start_url(["http://www.zone7.cn", "http://www.zone7.cn", "http://www.zone7.cn", "http://www.zone7.cn", ])
scrapy.parse()
Running example: (In order to facilitate everyone to read here, I added Chinese annotations, and there will be no annotations when actually running)
D:workenvScriptspython.exe D:/work_test/test/pdb_test/pdb_test.py
> d:work_test estpdb_testpdb_test.py(11)start_url()
-> for url in urls:
(Pdb) n Note: n ( next ) Under execution 1 Step
> d:work_test estpdb_testpdb_test.py(12)start_url()
-> print(url)
(Pdb) l Note: l ( list ) Lists the current code
7 urls = []
8
9 def start_url(self, urls):
10 pdb.set_trace()
11 for url in urls:
12 -> print(url)
13 self.urls.append(url)
14
15 def parse(self):
16 pdb.set_trace()
17 for url in self.urls:
(Pdb) c Note: c ( continue ), continue to execute until you encounter the following 1 Breakpoints
http://www.zone7.cn
http://www.zone7.cn
http://www.zone7.cn
http://www.zone7.cn
> d:work_test estpdb_testpdb_test.py(17)parse()
-> for url in self.urls:
(Pdb) n Note: n ( next ) Under execution 1 Step
> d:work_test estpdb_testpdb_test.py(18)parse()
-> result = self.request_something(url)
(Pdb) l Note: l ( list ) Lists the current code
13 self.urls.append(url)
14
15 def parse(self):
16 pdb.set_trace()
17 for url in self.urls:
18 -> result = self.request_something(url)
19
20 def request_something(self, url):
21 print('requesting...')
22 data = '''<!DOCTYPE html>
23 <html lang="en">
(Pdb) s Note: s ( step ) This is the entry request_something() Meaning of function
--Call--
> d:work_test estpdb_testpdb_test.py(20)request_something()
-> def request_something(self, url):
(Pdb) n Note: n ( next ) Under execution 1 Step
> d:work_test estpdb_testpdb_test.py(21)request_something()
-> print('requesting...')
(Pdb) l Note: l ( list ) Lists the current code
16 pdb.set_trace()
17 for url in self.urls:
18 result = self.request_something(url)
19
20 def request_something(self, url):
21 -> print('requesting...')
22 data = '''<!DOCTYPE html>
23 <html lang="en">
24 <head>
25 <meta charset="UTF-8">
26 <title>Title</title>
(Pdb) p url Note: p ( print ) Print out url Data of variables
'http://www.zone7.cn'
(Pdb) n Note: n ( next ) Under execution 1 Step
requesting...
> d:work_test estpdb_testpdb_test.py(31)request_something()
-> </html>'''
(Pdb) p data Note: p ( print ) Print out the data of the specified variable, where an error is reported because the assignment has not been completed
*** NameError: name 'data' is not defined
(Pdb) n Note: n ( next ) Under execution 1 Step
> d:work_test estpdb_testpdb_test.py(32)request_something()
-> return data
(Pdb) p data Note: p ( print ) Prints out the data for the specified variable
'<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
</body>
</html>'
(Pdb) q Note: q ( quit ) Exit
Summarize
According to the above example 1 set down, the basic usage can be learned, the key is to practice more, hands-on drills!