Command line debugging Python program __python

Source: Internet
Author: User

Reprint Address: https://blog.csdn.net/theonegis/article/details/52211799

Python provides a PDB for debugging tools similar to C + + GDB, and we can use the PDB under Linux to debug Python programs under the command line.
Official Reference website:
Python2:https://docs.python.org/2/library/pdb.html
Python3:https://docs.python.org/3/library/pdb.html

Generally, we can enter debugging in the following way (for example, the source file we want to debug is hello.py):
1. Start the target program at the command line, plus the-m parameter.
PYTHON-M PDB hello.py
The program will automatically stop at the first line, waiting for you to debug, as shown below. We can debug using debug commands, similar to using IED debugging.


2. Enable debugging in a python interactive environment
We can add the following code where the source file is to be debugged:

Import PDB  
pdb.set_trace ()


You can then automatically enter the PDB debug mode in an interactive environment, similar to the previous debugging method.

The following is a common debugging command (detailed information refer to the official website documentation):
Breakpoint Related commands:
To set a breakpoint: (PDB) b 8 #断点设置该文件的第8行 (b is the first letter of the break)
Show All Breakpoints: (Pdb) b #b命令, no parameters, show all breakpoints
Delete breakpoint: (PDB) CL 2 #删除第2个断点 (first letter of Clear)

Step over: (PDB) n #单步执行, next first letter
Step into: (PDB) s #step的首字母
SETP return: (Pdb) R #return的首字母
Resume: (Pdb) C #continue的首字母
Run To Line: (PDB) J #运行到地10行, Jump's first letter

(Pdb) p param #查看当前param变量值
(PDB) L #查看运行到某处代码
(PDB) A #查看全部栈内变量

(Pdb) H #帮助, first letter of Help
(Pdb) Q #退出, first letter of quit


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.