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

Python debugging: logging and pdb (example analysis 2)

Aug 23, 2018 pm 01:38 PM
python debug

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to efficiently integrate Node.js or Python services under LAMP architecture? How to efficiently integrate Node.js or Python services under LAMP architecture? Apr 01, 2025 pm 02:48 PM

Many website developers face the problem of integrating Node.js or Python services under the LAMP architecture: the existing LAMP (Linux Apache MySQL PHP) architecture website needs...

What is the reason why pipeline persistent storage files cannot be written when using Scapy crawler? What is the reason why pipeline persistent storage files cannot be written when using Scapy crawler? Apr 01, 2025 pm 04:03 PM

When using Scapy crawler, the reason why pipeline persistent storage files cannot be written? Discussion When learning to use Scapy crawler for data crawler, you often encounter a...

Python Cross-platform Desktop Application Development: Which GUI Library is the best for you? Python Cross-platform Desktop Application Development: Which GUI Library is the best for you? Apr 01, 2025 pm 05:24 PM

Choice of Python Cross-platform desktop application development library Many Python developers want to develop desktop applications that can run on both Windows and Linux systems...

What is the reason why the Python process pool handles concurrent TCP requests and causes the client to get stuck? What is the reason why the Python process pool handles concurrent TCP requests and causes the client to get stuck? Apr 01, 2025 pm 04:09 PM

Python process pool handles concurrent TCP requests that cause client to get stuck. When using Python for network programming, it is crucial to efficiently handle concurrent TCP requests. ...

How to view the original functions encapsulated internally by Python functools.partial object? How to view the original functions encapsulated internally by Python functools.partial object? Apr 01, 2025 pm 04:15 PM

Deeply explore the viewing method of Python functools.partial object in functools.partial using Python...

Python hourglass graph drawing: How to avoid variable undefined errors? Python hourglass graph drawing: How to avoid variable undefined errors? Apr 01, 2025 pm 06:27 PM

Getting started with Python: Hourglass Graphic Drawing and Input Verification This article will solve the variable definition problem encountered by a Python novice in the hourglass Graphic Drawing Program. Code...

How to optimize processing of high-resolution images in Python to find precise white circular areas? How to optimize processing of high-resolution images in Python to find precise white circular areas? Apr 01, 2025 pm 06:12 PM

How to handle high resolution images in Python to find white areas? Processing a high-resolution picture of 9000x7000 pixels, how to accurately find two of the picture...

How to efficiently count and sort large product data sets in Python? How to efficiently count and sort large product data sets in Python? Apr 01, 2025 pm 08:03 PM

Data Conversion and Statistics: Efficient Processing of Large Data Sets This article will introduce in detail how to convert a data list containing product information to another containing...

See all articles