Python learn the necessary debug artifact -pdb
table of Contents
- Hundred thousand forced hundred
- Use Introduction
- Here is a simple chestnuts
Hundred thousand forced hundred
First of all, tell us about debugging pdb, pdb python is a built-in module for command line debugging Python code. Perhaps you will say, now with Pycharm and other editor to debug code is very easy, why use the command line it? This problem, I used to think so, until one time, the code must be run on a Linux system (now Pycharm can remotely debug the code, and today I will not speak of this)
Use Introduction
How to add a breakpoint?
When it comes to debug, be sure to add a breakpoint, there are two ways to add breakpoints:
- Want to add a line break after the code
pdb.set_trace()
If using this method, you can enter directly run the Python file breakpoint debugging.
- Use the command line to add a breakpoint
b line_number (lines of code)
If using this method, you need to start python -m pdb xxx.py breakpoint debugging.
Common Commands
Briefly explain the use of command here do not remember, and so used to come back to check on the line.
1 into the command line Debug mode, python -m pdb xxx.py
2 h: (help) help
3 w: (where) Print execution stack
4 d: (down) execution jumps to a deeper level in the current stack (personally did not feel anything useful)
5 u: (up) jumps to execute on the current stack layer
6 b: (break) add breakpoint
b lists all current breakpoints, breakpoints and execution to count the number of
b line_no: line_no current script line to add a breakpoint
b filename: line_no: line_no line of the script filename to add a breakpoint
b function: can function in the first function of Add a breakpoint at the statement execution
7 tbreak: (temporary break) temporary breakpoint
After the first execution to the breakpoint, the breakpoint is automatically deleted, as usage and b
8 cl: (clear) to clear the breakpoint
Clear all breakpoints cl
cl bpnumber1 bpnumber2 ... clear the breakpoint number bpnumber1, bpnumber2 ... breakpoints
cl lineno clear the current script lineno line break
cl filename: line_no clear breakpoints line_no line of script filename
9 disable: Disable breakpoint parameters bpnumber, and cl difference is that the breakpoint still exists, but is not enabled
10 enable: activation breakpoints parameters bpnumber
11 s: the (step) executes a command
If this sentence is a function call, then s will perform the function of the first sentence
12 n: the (next) one statement
If this sentence is a function call, the function is executed, then a currently executing the next statement.
13 r: (return) to the end of the execution of the current operating function
14 c: (continue) continue to execute until the next breakpoint
15 l: (list) Source List
l lists the currently executing statement 11 surrounding the code
l around the first listed first line of code 11
l SECOND first listed first - second range of codes, the number of lines if the second <first, second will be interpreted as
16 a: (args) lists the function of the current execution of the function
17 p expression: Value (print) of the output expression
18 pp expression: a little good-looking p expression
19 run: Restart debug, equivalent to restart
20 q: (quit) quit debug
21 j lineno: (jump) set statement function performed under Article
Only at the very bottom of the stack to jump backwards again executed, can be executed directly to the forward line number
22) unt: (until) to the next line (out of the loop), or the end of the current stack
23) condition bpnumber conditon, to set a breakpoint condition, condition returns True if the argument when bpnumber breakpoint is valid, otherwise invalid breakpoint bpnumber
Here is a simple chestnuts
In order to verify the usage of pdb, I wrote a simple Python code as follows:
__author__ = 'zone'
__gzh__ = '公号: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 instance here in order to facilitate reading, I added the Chinese comments, there will be no actual annotated runtime)
D: Work envScriptspython.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) the n-Notes: n (next) to perform Next
> D: work_test estpdb_testpdb_test.py (12 is) START_URL ()
-> Print (URL)
(Pdb) L Note: l (list) lists the current code
. 7 URLs = []
. 8
. 9 DEF START_URL (Self, URLs):
pdb.set_trace 10 ()
. 11 for URLs in URL:
12 is -> Print (URL)
13 is self.urls.append (URL)
14
15 DEF the parse (Self):
16 pdb.set_trace ()
. 17 for URL in self.urls:
(Pdb) c Notes: c (continue), continue, know next breakpoint is encountered
http://www.zone7.cn
http://www.zone7.cn
http://www.zone7.cn
http://www.zone7.cn
> D: work_test estpdb_testpdb_test.py (. 17) the parse ()
-> URL in self.urls for:
(Pdb) n-Note: n (next) to the next step
> d: work_test estpdb_testpdb_test. Py (18 is) the parse ()
-> Result = self.request_something (URL)
(Pdb) L Note: l (list) lists the current code
13 is self.urls.append (URL)
14
15 DEF the parse (Self):
16 PDB .set_trace ()
. 17 for URL in self.urls:
18 is -> Result = self.request_something (URL)
. 19
20 is DEF request_something (Self, URL):
21 is Print ( 'Requesting ...')
22 is Data = '' '< ! DOCTYPE HTML>
23 is <HTML lang = "EN">
(Pdb) S Note: s (step) herein is meant to enter request_something () function
--Call--
> d:work_test estpdb_testpdb_test.py(20)request_something()
-> def request_something(self, url):
(Pdb) n 注释:n(next)执行下一步
> d:work_test estpdb_testpdb_test.py(21)request_something()
-> print('requesting...')
(Pdb) l 注释: l(list)列出当前代码
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) n-Note: n (next) to the next step'http://www.zone7.cn'
(Pdb) p url NOTE: p (print) a print variable data url
... Requesting
> D: work_test estpdb_testpdb_test.py (31 is) request_something ()
-> </ HTML> '' '
(Pdb) P Data NOTE: p (print) a print data specified variable, where the assignment is not yet complete because Therefore given
*** NameError: name 'Data' IS Not defined
(Pdb) n-Note: n (next) to the next step
> D: work_test estpdb_testpdb_test.py (32) request_something ()
-> return Data
(Pdb) Data P Note: p (print) the print data from a specified variable
'<DOCTYPE HTML!> <HTML lang = "EN"> <head> <Meta charset = "UTF-. 8"> <title> the title </ title> </ head > <body> </ body> </ HTML> '
(Pdb) Q Note: q (quit) exit
Summary in accordance with the above example set down the basic usage can be learned, the key is to get yourself more practice, hands-on practice!