Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to debug Python under Linux

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to debug Python under Linux, I believe that many inexperienced people are at a loss about this. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Generally speaking, developers debug their programs in IDE. Of course, if there is IDE, IDE is the first choice for debugging.

However, sometimes our business scenario limits debugging to Linux command-line mode.

What should I do at this time?

We introduce a tool for debugging Python programs under Linux.

Introduction to pdb

The pdb debugger is provided by the Python standard library, so it is most convenient and can be used directly after import without the need to install other components.

The pdb debugger provides most of the features needed for debugging, such as breakpoints, one-way walking, stack frame checking, and so on.

Common command

L # check which line of code to run to

N # step by step, skip function

S # step by step, you can enter the function

P variable # View variable value

B line number # breakpoint set to which line

B # display a list of all breakpoints

Cl breakpoint # Delete a breakpoint

Cl # remove all breakpoints

C # Jump to the next breakpoint

R # return current function

Exit # exit

Copy the code

Use the example Python environment of this article: Python 3.5.2

Let's prepare a short demo program first:

#-*-coding: utf-8-*-

Def add (a, b):

Return a + b

If _ _ name__ = ='_ _ main__':

Print ("= start===")

C = add (1,3)

Print ("= end===")

Copy the code

Use method 1:

The easiest way to run pdb is to debug the program as an argument from the command line.

$python-m pdb test_pdb.py

Copy the code

At this point, step-by-step execution begins.

This approach is not intrusive to the code, but breakpoints need to be set each time.

Method 2:

Introduce pdb in the code header, and then you can set the breakpoint through pdb.set_trace () in the code:

#-*-coding: utf-8-*-

Import pdb

Def add (a, b):

Pdb.set_trace ()

Return a + b

If _ _ name__ = ='_ _ main__':

Print ("= start===")

Pdb.set_trace ()

C = add (1,3)

Print ("= end===")

Copy the code

At this point, when you run the program, you will automatically jump to the set breakpoint:

Without the graphical page, debugging can only come this way, but fortunately, it is not difficult to use pdb.

There are also enhanced debuggers, such as IPython's ipdb and pdb++, which generally provide a better user experience, adding useful additional features such as syntax highlighting, better backtracking, and introspection.

After reading the above, have you mastered how to debug Python under Linux? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 225

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report