"Python debugger" rapid positioning of various incurable diseases! !

Now many of the editors actually with "debugger" function, such as writing c / c ++ is codeblocks, write Python's pycharm, this graphical interface to use and display are quite friendly and easy to learn, this is not me this articles talk about focus. The main today is to tell you about the "Python debugger" to quickly locate various incurable diseases.

Python Debugger

In this part, just to say two Python debugger, are standard library comes with pdb and open source ipdb.

pdb

pdb Python comes with a library that provides an interactive Python source code debugging capabilities, including the current debugger proper functions, including setting breakpoints, single-step debugging, viewing the source code and so on. In fact, if you had learned before c / c ++, you probably know this command-line gdb debugging tool, if you previously used gdb, then congratulations, you, you can directly use pdb, because a usage of these two brothers. If you do not know gdb all right, let's look at the part pdb debug command:

 

There are two different ways to start Python debugger, two methods suitable for different scenarios. One is directly on the command line parameter specifies the start Python pdb file module, which is adapted to short code file, the Python debugger will start the first line of code. As shown below (e.g., file name is test.py):


python -m pdb test.py

. 1
2
. 3
set_trace Another method is to call the module in the Python code pdb set a breakpoint, the breakpoint when this program is running, the program will be suspended and the open pdb debugger, which is adapted to more code files large. As shown below:

import pdb

def get_sum(n):
cnt = 0
for i in range(n):
pdb.set_trace()
cnt += i
print(cnt)

if __name__ == '__main__':
get_sum(5)

. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
after the start Python debugger can debug the screenshot above commands debugger. For example, in the next operation, we first use the list to see our code, and then print using the current values of the variables p, and finally the next line of Python code with the n:

 

ipdb

ipdb is an open source Python debugger, pdb interface and in fact it is the same. That being the same interface that why should design a ipdb it? Hegel once said, "there is reasonable", ipdb compared to more than pdb syntax highlighting, tab automatically fill congruent friendly features, do a lot of improvements in ease of use, this feeling and Python and IPython same.

Of course, ipdb as a third-party library, you must install before using the inevitable:

pip install ipdb

1
2
we change it before test.py file example, the following code is modified as follows:

import ipdb

def get_sum(n):
cnt = 0
for i in range(n):
ipdb.set_trace()
cnt += i
print(cnt)

if __name__ == '__main__':
get_sum(5)

. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
the specific operation or operations and the same as above pdb:
--------------------- 

Guess you like

Origin www.cnblogs.com/hyhy904/p/10942246.html