Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of Python code debugging

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what is the method of Python code debugging". In the daily operation, I believe that many people have doubts about the method of Python code debugging. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what is the method of Python code debugging?" Next, please follow the editor to study!

Listing 1. Test code example

Import pdba = "aaa" pdb.set_trace () b = "bbb" c = "ccc" final = a + b + cprint final

Start debugging: run the script directly and stay at pdb.set_trace (). Select n+enter to execute the current statement. After pressing n+enter for the first time, you can press enter to repeat the previous debug command.

Listing 2. Debugging with pdb

[root@rcc-pok-idg-2255] # python epdb1.py > / root/epdb1.py (4)? ()-> b = "bbb" (Pdb) n > / root/epdb1.py (5)? ()-> c = "ccc" (Pdb) > / root/epdb1.py (6)? ()-> final = a + b + c (Pdb) list1 import pdb2 a = "aaa" 3 pdb.set_trace () 4 b = "bbb" 5c = "ccc" 6-> final = A + b + c7 print final [EOF] (Pdb) [EOF] (Pdb) n > / root/epdb1.py (7)-> print final (Pdb)

Exit debug: you can exit the current debug using quit or Q, but quit exits the program in a very rude way, resulting in a direct crash.

Listing 3. Exit debug

[root@rcc-pok-idg-2255] # python epdb1.py > / root/epdb1.py (4)? ()-> b = "bbb" (Pdb) n > / root/epdb1.py (5)? ()-> c = "ccc" (Pdb) qTraceback (most recent call last): File "epdb1.py", line 5, in? C = "ccc" File "epdb1.py", line 5, in? C = "ccc" File "/ usr/lib64/python2.4/bdb.py", line 48 In trace_dispatchreturn self.dispatch_line (frame) File "/ usr/lib64/python2.4/bdb.py", line 67, in dispatch_lineif self.quitting: raise BdbQuitbdb.BdbQuit

Print the value of the variable: if you need to print the value of the variable during debugging, you can directly use p plus the variable name, but it is important to note that printing can only see the specific value after the current statement has been executed, otherwise the NameError will be reported:

< exceptions.NameError … ....>

Mistake.

Listing 4. Printing variables during debug

[root@rcc-pok-idg-2255] # python epdb1.py > / root/epdb1.py (4)? ()-> b = "bbb" (Pdb) n > / root/epdb1.py (5)? ()-> c = "ccc" (Pdb) p bbbbbbb' (Pdb) 'bbb' (Pdb) n > / root/epdb1.py (6)? ()-> final = a + b + c (Pdb) p cccc' (Pdb) p final*** NameError: (Pdb) n > / root/epdb1.py (7)? ()-> print final (Pdb) p final'aaabbbccc' (Pdb)

Use c to stop the current debug and keep the program running. If you continue to declare set_statement () in the following program, you will re-enter the debug state, and the reader can add set_trace () validation before the code print final.

Listing 5. Stop debug and continue to execute the program

[root@rcc-pok-idg-2255] # python epdb1.py > / root/epdb1.py (4)-> b = "bbb" (Pdb) n > / root/epdb1.py (5)? ()-> c = "ccc" (Pdb) caaabbbccc

Display code: you may not be able to remember the current code block when you debug. If you want to see a specific code block, you can display it by using the list or l command. List uses the arrow-> to point to the statement of the current debug.

Listing 6. Display code during debug

[root@rcc-pok-idg-2255] # python epdb1.py > / root/epdb1.py (4)? ()-> b = "bbb" (Pdb) list1 import pdb2 a = "aaa" 3 pdb.set_trace () 4-> b = "bbb" 5c = "ccc" 6 final = a + b + c7 pdb.set_trace () 8 print final [EOF] (Pdb) c > / root/epdb1.py (8)-> print final (Pdb) list3 pdb.set_trace ( ) 4 b = "bbb" 5 c = "ccc" 6 final = a + b + c7 pdb.set_trace () 8-> print final [EOF] (Pdb)

Debug with functions

Listing 7. Examples of using functions

Import pdbdef combine (S1 sandwiches S2): # define subroutine combine, which...s3 = S1 + S2 + S1 # sandwiches S2 between copies of S1,... S3 ='"+ S3 +'"'# encloses it in double quotes,...return S3 # and returns it.a = "aaa" pdb.set_trace () b = "bbb" c = "ccc" final = combine (Azob) print final

If you directly use n for debug, then it will be treated as an ordinary assignment statement when you go to final=combine (ajar b) and enter into print final. What if you want to debug a function? You can use s directly to enter the function block. The single-step debugging in the function is similar to the above description. If you do not want to single-step debugging in the function, you can press r at the breakpoint to exit to the place of the call.

Listing 8. Debug the function

[root@rcc-pok-idg-2255 ~] # python epdb2.py > / root/epdb2.py (10)? ()-> b = "bbb" (Pdb) n > / root/epdb2.py (11)? ()-> c = "ccc" (Pdb) n > / root/epdb2.py (12)? ()-> final = combine (AMague b) (Pdb) smurf Calltel-> / root/epdb2.py (3) combine ()-> def combine (s1Magazine S2): # define subroutine combine Which... (Pdb) n > / root/epdb2.py (4) combine ()-> S3 = S1 + S2 + S1 # sandwiches S2 between copies of S1,... (Pdb) list1 import pdb23 def combine (S1 between copies of S2): # define subroutine combine, which...4-> S3 = S1 + S2 + S1 # sandwiches S2 between copies of S1,... 5s3 =''+ S2 +'"''# encloses it in double quotes 6 return S3 # and returns it.78 a = "aaa" 9 pdb.set_trace () 10 b = "bbb" 11 c = "ccc" (Pdb) n > / root/epdb2.py (5) combine ()-> S3 ='"+ S3 +'"'# encloses it in double quotes (Pdb) n > / root/epdb2.py (6) combine ()-> return S3 # and returns it. (Pdb) NMurashi Returnmuri-> / root/epdb2.py (6) combine () >'"aaabbbaaa"'- > return S3 # and returns it. (Pdb) n > / root/epdb2.py (13)-> print final (Pdb)

Change the value dynamically during debugging. The value of the variable can be changed dynamically when debugging, as shown in the following example. It should be noted that there is an error below because b has been assigned, so if you want to change the assignment of b again, you should use it! B .

Listing 9. Dynamically change the value during debugging

[root@rcc-pok-idg-2255 ~] # python epdb2.py > / root/epdb2.py (10)? ()-> b = "bbb" (Pdb) var = "1234" (Pdb) b = "avfe" * * The specified object'= "avfe"'is not a functionor was not found along sys.path. (Pdb)! B = "afdfd" (Pdb)

Pdb debugging has an obvious defect is that the support for multithreading and remote debugging is not good enough, and there is no more intuitive interface display, which is not suitable for large-scale python projects. In larger python projects, these debugging requirements are common, so you need to use more advanced debugging tools.

At this point, the study on "what is the method of Python code debugging" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report