Home  >  Article  >  Backend Development  >  How to debug Python? How to debug python?

How to debug Python? How to debug python?

little bottle
little bottleOriginal
2019-05-18 11:04:0110387browse

Debug is a very important debugging skill in coding. By setting breakpoints during the running process, it can help developers better understand the running process. Debugging in Python is not as intuitive as setting breakpoints in the IDE like JAVA or C. Please follow the editor to find out more about it below.

How to debug Python? How to debug python?

There are two ways to debug Python: 1. Run in the command line, 2. Run in the script. Both methods require the use of the pdb module.

Method 1: Run in the command line

$ python -m pdb my_script.py

Method 2: Run in the script

When needed Where the breakpoint is set, insert the method pdb.set_trace()

import pdbdef make_bread():
    pdb.set_trace()    return "I don't have time"print(make_bread())

command: After entering the debugging state, you can enter the command for debugging.

c: (continue)继续执行
w:(words)显示当前行的上下文信息
a:(arguments)打印当前函数的参数列表
s:(stop)执行当前行,并在顶一个可能的时机停止
n:(next)继续执行直到当前函数的下一行或者函数返回值

The above is the detailed content of How to debug Python? How to debug python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn