In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces Python essential debug artifact pdb how to use, the content is very detailed, interested friends can refer to, hope to be helpful to you.
A necessary debug artifact for learning Python-pdb
First, let's introduce pdb debugging, a built-in module of python that is used to debug Python code on the command line. You might say that it's convenient to debug code with editors such as Pycharm, so why use the command line? I used to think the same way about this problem, until once, the code had to run on the Linux system (now Pycharm can debug the code remotely, let's not talk about it today)
Introduction to use
How do I add breakpoints?
When it comes to debug, you must add breakpoints. There are two ways to add breakpoints:
Add a line after you want the breakpoint code
Pdb.set_trace ()
If you use this method, you can enter breakpoint debugging by running the Python file directly.
Use the command line to add breakpoints
B line_number (lines of code)
If you use this approach, you need python-m pdb xxx.py to start breakpoint debugging.
Common command
First of all, a brief introduction to the use of commands, there is no need to remember here, and so on when you need to come back to check.
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) execution jumps to a deeper layer of the current stack (personally I don't think it's useful)
5 u: (up) performs a jump to the upper layer of the current stack
6 b: (break) add breakpoints
B list all current breakpoints, and the number of breakpoints executed to statistics b line_no: current script line_no line add breakpoints b filename:line_no: script filename line_no lines add breakpoints b function: add breakpoints at the first executable statement of function function
7 tbreak: (temporary break) temporary breakpoint
After the breakpoint is executed for the first time, the breakpoint is automatically deleted, using the same as b
8 cl: (clear) clear breakpoint
Cl clears all breakpoints cl bpnumber1 bpnumber2... Clear the breakpoint number as bpnumber1,bpnumber2... Cl lineno clears the breakpoint of the current script lineno line cl filename:line_no clears the breakpoint of the line_no line of the script filename
9 disable: disable the breakpoint. The parameter is bpnumber. The difference with cl is that the breakpoint still exists, but it is not enabled.
10 enable: activate the breakpoint. Parameter is 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) execute the next statement
If this sentence is a function call, the function is executed, followed by the next item of the currently executed statement.
13 r: (return) executes the currently running function to the end
14 c: (continue) continue execution until the next breakpoint is reached
15 l: (list) list the source code
L lists 11 codes around the currently executed statement l first lists 11 codes around first lines l first second lists first--second range codes If second d:work_test estpdb_testpdb_test.py (11) start_url ()-> for url in urls: (Pdb) n comment: n (next) perform next > d:work_test estpdb_testpdb_test.py (12) start_url ()-> print (url) (Pdb) l comment: 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 the next breakpoint http://www.zone7.cnhttp://www.zone7.cnhttp://www.zone7.cnhttp://www.zone7.cn> d:work_test estpdb_testpdb_test.py (17) parse ()-> for url in self.urls: (Pdb) n Note: n (next) perform the next 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 =''23 (Pdb) s Note: s (step) here means to enter the request_something () function-- Call-- > d:work_test estpdb_testpdb_test.py (20) request_something ()-> def request_something (self) Url): (Pdb) n comment: n (next) execute next > d:work_test estpdb_testpdb_test.py (21) request_something ()-> print ('requesting...') (Pdb) l comment: 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 =' 23 24 25 26 Title (Pdb) p url comment: P (print) print out the data of the url variable 'http://www.zone7.cn'(Pdb) n comment: n (next) perform the next step requesting... > d:work_test estpdb_testpdb_test.py (31) request_something ()->' (Pdb) p data comment: P (print) print out the data of the specified variable Since the assignment has not been completed here, an error has been reported * NameError: name 'data' is not defined (Pdb) n comment: n (next) perform the next step > d:work_test estpdb_testpdb_test.py (32) request_something ()-> return data (Pdb) p data comment: P (print) print out the data of the specified variable' Title'(Pdb) Q comment: Q (quit) exit on Python essential debug artifact pdb how to share here I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.