Python debugging in Linux

Source: Internet
Author: User

Python debugging in Linux

Python has its own pdb library for simple debugging. The basic commands are similar to gdb, but the functions are not as powerful as gdb. pdb mainly supports multi-breakpoint settings (conditional settings ), code-level single-step debugging, viewing stack information, code viewing, post-mortem debugging,

For more information, see: http://docs.python.org/2/library/pdb.html


There are multiple ways to debug with pdb:

1. Run the command line to start the target program and add the-m parameter. In this way, if myscript. py is called, the breakpoint is before the execution of the program.
Python-m pdb myscript. py


2. Enable debugging in the Python interaction environment
>>> Import pdb
>>> Import mymodule
>>> Pdb. run ('mymodule. test ()')


3. It is commonly used to insert a program in the middle of the program, as opposed to hitting a breakpoint in the general IDE and then start debug,

[Python] view plaincopy print?
  1. If _ name __= = "_ main __":
  2. A = 1
  3. Importpdb
  4. Pdb. set_trace ()
  5. B = 2
  6. C = a + B
  7. Print (c)

After the script runs properly, it will be fixed at pdb. set_trace (), and the debugging prompt (Pdb) will be displayed.

Some Common commands:

H (elp) [comman] # print available commands and help information

R (eturn) # Run the code until the next breakpoint or the current function returns

B (reak) [[filename:] lineno | function [, condition] # specify a row or function body of the file to set breakpoints

L (ist) [first [, last] # view a specified code segment

N (ext) # execute the next line

S (tep) # execute the next line. If it is a function, it enters the function body.

P # print a variable

A (rgs) # print the parameters of the current function

W (here) # print stack information

D (own) # Move to the lower stack

U (p) # Move to the upper stack

J (ump) # Jump to the specified row

Continue/c # continue execution

Disable [bpnumber [bpnumber] # failure breakpoint

Enable [bpnumber [bpnumber] # enable breakpoint

Cl (ear) [filename: lineno | bpnumber [bpnumber] # delete a breakpoint

Q (uit)/exit # Stop debugging and exit

Related Article

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.