Home > Backend Development > Python Tutorial > Python debugging: logging and pdb (example analysis 2)

Python debugging: logging and pdb (example analysis 2)

乌拉乌拉~
Release: 2018-08-23 13:38:08
Original
1411 people have browsed it

In the previous article, we learned why python needs to be debugged, and introduced two methods of python debugging. However, the debugging method cannot be explained in one article, so in this article we will talk about it. Let’s look at the remaining two debugging methods. I hope these debugging methods can be helpful to you and help you move forward more quickly on the road to learning python.

logging

The third method is to replace print() with logging. Compared with assert, logging will not throw an error, and It can be output to a file:

import logging
s = '0'
n = int(s)
logging.info('n = %d' % n)
print(10 / n)
Copy after login

logging.info() can output a piece of text. Run and find no information except ZeroDivisionError. what happened?

Don’t worry, add a line of configuration after import logging and try again:

import logging
logging.basicConfig(level=logging.INFO)
Copy after login

See the output:

$ python err.py
INFO:root:n = 0
Traceback (most recent call last):
  File "err.py", line 8, in <module>
    print(10 / n)
ZeroDivisionError: division by zero
Copy after login

This is the benefit of logging, it allows you to specify records The levels of information include debug, info, warning, error, etc. When we specify level=INFO, logging.debug will not work. In the same way, after specifying level=WARNING, debug and info will not work. In this way, you can safely output different levels of information without deleting it, and finally control which level of information is output.

Another benefit of logging is that through simple configuration, a statement can be output to different places at the same time, such as the console and files.

pdb

The fourth way is to start the Python debugger pdb, let the program run in single-step mode, and you can check the running status at any time . We first prepare the program

# err.py
s = &#39;0&#39;
n = int(s)
print(10 / n)
Copy after login

and then start it:

$ python -m pdb err.py
> /Users/michael/Github/learn-python3/samples/debug/err.py(2)<module>()
-> s = &#39;0&#39;
Copy after login

After starting with the parameter -m pdb, pdb locates the code to be executed next -> s = '0' . Enter the command l to view the code:

 (Pdb) l
  1     # err.py
  2  -> s = &#39;0&#39;
  3     n = int(s)
  4     print(10 / n)
Copy after login

Enter the command n to step through the code:

(Pdb) n
> /Users/michael/Github/learn-python3/samples/debug/err.py(3)<module>()
-> n = int(s)
(Pdb) n
> /Users/michael/Github/learn-python3/samples/debug/err.py(4)<module>()
-> print(10 / n)
Copy after login

You can enter the command p variable name at any time to view the variable:

(Pdb) p s
&#39;0&#39;
(Pdb) p n
0
Copy after login

Enter the command q to end debugging and exit the program:

(Pdb) q
Copy after login

(This method of debugging on the command line through pdb is theoretically omnipotent, but it is really too troublesome)

That’s it for this article For all the content described in the article, this article mainly introduces the relevant knowledge of python debugging. I hope you can use the information to understand the above content. I hope what I have described in this article will be helpful to you and make it easier for you to learn python.

For more related knowledge, please visit the Python tutorial column on the php Chinese website.

The above is the detailed content of Python debugging: logging and pdb (example analysis 2). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template