Debugging a Python script using the PDB module

Source: Internet
Author: User
Tags define function python script

in Python, syntax errors can be found by the Python interpreter, but logical errors or variable usage errors are not easy to find, and if the results do not meet expectations, debugging is a good debugging tool: The PDB module that comes with Python. The PDB is the debug module that comes with Python. Use the PDB module to set breakpoints, step through, view variable values, and so on for a script.

The PDB can be started with command-line arguments, or it can be imported and reused using import.

>>> dir (PDB) [' PDB ', ' repr ', ' Restart ', ' testCMD ',....., ' re ', ' Run ', ' Runcall ', ' runctx ', ' runeval ', ' set_ Trace ', ' sys ', ' Test ', ' traceback ']

The common PDB functions are as follows:

"Pdb.run () function "

>>> This function is primarily used to debug statement blocks

>>> basic usage is as follows

>>> Help (Pdb.run) Help on Function run in module Pdb:run (statement, Globals=none, Locals=none)

>>> parameter meaning

statement: The block of statements to be debugged, expressed as a string

globals: optional parameter to set global environment variables for statement run

locals: optional parameter to set local environment variables for statement run

>>> Simple Example

>>> import PDB                # importing Debug Modules >>> Pdb.run ("               # Call the Run () function to execute a for loop for I in range (3):    I *= 3    Print (i) "    ) > <string> (2) <module> ()     (PDB) n                       # (PDB) is a debug command prompt that indicates that you can enter debug commands > <string > (3) <module> () (PDB) n                       # N (Next) indicates execution of the next line > <string> (4) <module> () (PDB) print (i)                # Print the value of the variable i 0 (Pdb) Continue                # continue running Program 036

"pdb.runeval () function "

>>> This function is primarily used for debugging expressions

>>> basic usage is as follows

>>> Help (Pdb.runeval) to the function runeval in module pdb:runeval (expression, Globals=none, Locals=none)
>>> parameter meaning

expression: to debug,

globals: optional parameter to set global environment variables for statement run

locals: optional parameter to set local environment variables for statement run

>>> Simple Example
>>> import PDB                  # importing PDB module >>> LST = [1, 2, 3]             # Defining a list >>> Pdb.runeval (' lst[1] ')       # Call Runaval () function to debug the expression lst[1]> <string> (1) <module> () (PDB) n                         # Enter debug state, use the n command, step into--return--> < String> (1) <module> ()->2 (PDB) n                         # stepping 2                               # Returns the value of the expression >>> pdb.runeval (' 3 + 5*6/2 ')    # Use the Runaval () function to debug an expression 3+5*6/2> <string> (1) <module> ()->2 (Pdb) n--return--> <string> (1) < Module> ()->18 (PDB) n                         # uses the n command                              to step through the value of the last expression
" Pdb.runcall () function"

>>> This function is primarily used for debugging functions

>>> basic usage is as follows

>>> Help (Pdb.runcall) to the function Runcall in module Pdb:runcall (*args, **kwds)
>>> parameter meaning
function: name of function

args (Kwds):         function parameters
>>> Simple example

>>> import PDB                           # import module >>> def sum (*args):                      # define function sum, find all arguments with res = 0for arg in Args:res + = Argretur n res>>> pdb.runcall (sum, 1, 2, 3, 4)         # Use Runcall debug function sum> <pyshell#53> (2) sum () (PDB) n                                  # Enter debug state , Step Into > <pyshell#53> (3) sum (PDB) n                                  # Stepping > <pyshell#53> (4) sum () (       pdb) print (res)                         # Print Res value 0 (Pdb) with print continue                           # Continue execution 10>>> pdb.runcall (sum, 1, 2, 3, 4, 5, 6)   # call Runcall debug function sum with different arguments > <pyshell#53> (2) sum () (               Pdb) Continue                           # continue to execute the                                       # function and return the result
" pdb.set_trace () function"

>>> This function is primarily used to set a hard breakpoint in a script

>>> basic usage is as follows

>>> Help (Pdb.set_trace) to the function set_trace in module pdb:set_trace ()
>>> Simple Example
# File:test.pyimport Pdbpdb.set_trace () for I in range (5):    i *= 5    print (i)
after running the script, it displays:
> d:\learn\python\test.py (6) <module> ()-for-I in range (5):(Pdb) List                     # List script contents using list  1  # File: test.py  2    3  import pdb  4    5  pdb.set_trace ()        # Use Set_trace () to set a hard breakpoint  6,  For I in range (5):  7      i *= 5  8      Print (i) [EOF]                          # list script content end (PDB) Continue                 # Continue with Continue 05101520

" pdb debug Command "

The debug commands in the PDB can complete stepping, printing variable values, setting breakpoints, and more. The PDB main commands are as follows

------------------------------------------------------------------------------# full Command shorthand command                            Description------------------------------------------------------------------------------# args A                        Print parameters for the current function # Break B set Breakpoint # Clear                     CL Clear Breakpoint # Condition no set conditional breakpoint # continue C continues to run until a breakpoint is encountered or the script ends # Disable no disable                           Breakpoint # Enable no breakpoint enabled # Help H                            View PDB help # Ignore no ignore breakpoint # Jump J                            Jump to a specified number of lines run # list L List script List # next nExecutes the next statement, encounters a function that does not enter its internal # print p printing variable Value # quit Q                       Exit pdb# return R runs uniformly to function return # Tbreak No temporary breakpoint is set, breakpoint is interrupted only once # step S executes the next statement, encounters a function into its interior                           # where w see where you are #!                     No statements are executed in the PDB



Debugging a Python script using the PDB module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.