How to efficiently debug Python programs?

Python's debug support is good. When the meaning of the code is clear, it can be very efficient to analyze the cause of the error through log, print and assert, and cooperate with unit testing. However, in actual work, a lot of code is likely to be written by others, and in this case, it is more efficient to use the debugger.

 

 

 

 

1. Debug the program under the console

 

PDB

 

If you are familiar with command-line debugging tools (such as gdb, lldb), you will have a very good experience using PDB in Python. PDB not only supports calling when the project starts, but also supports interactive debugging in the Python shell; functionally, it supports Breakpoints, stepping, exception capture and resolution, variable viewing, variable overwriting, stack viewing and even bytecode viewing, etc.

 

An example of PDB breakpoint debugging:

Create a new document with a text editor, name it debug1.py, and enter the simplest Python code snippet:

import pdb
a = "a string"
b= "b string"
pdb.set_trace()
print("next step")

Then run the script, Windows runs like this:

python debug1.py

Then after executing the sentence pdb.set_trace(), it will automatically enter the breakpoint debugging mode, and the screen will display similar information:

> c:\users\david\documents\debug1.py(5)<module>()
-> print("next step")
(pdb)

PDB displays the current breakpoint location, and then you can use PDB debug commands.

 

 

 

 

Tips: Other common commands are:

pp, print

n, the next step, execute the next step

s, step, step by step execution

l, list, show source code around breakpoints

c, continue, continue the operation of the program

r, return, continue until the current function returns

 

The VIM and Emacs tools introduced by PDB can well achieve effects similar to lldb and gdb. At the same time, combined with the extended function shell of IPython, the magic commands in it can better help program debugging. This is a common debugging combination in the general *nix environment, and it can achieve a good IDE-like experience with well-configured text editing tools. PDB is more capable of remote debugging, which is to debug the remote end (such as the Python code running on the server). Almost all debugger tools improve the user interface based on the functions of PDB.

 

In addition, for some frameworks, especially web frameworks, debugging often needs to be combined with a specific context (for example, Django's development, debugging and testing requires context, etc.), PDB interactive debugging can be directly mounted in the corresponding interactive environment (such as Django shell).

 

Pudb

 

If you think this is too primitive, you can try pudb, which is a console-based debug graphical debugger tool, slightly more intuitive than pdb, but only works under Linux.

 

 

This GUI is a bit primitive and doesn't support the mouse, so don't use the mouse to click.

 

 

2. Program debugging under the graphical interface

 

If you're more used to the IDE's overall debugging, no problem. Many IDEs that support Python have very powerful debugging functions, and even lightweight editors have complete debugging functions. Next, we briefly introduce several graphical IDE tools:

 

Visual Studio Code

 

As a cross-platform heavyweight text editor and lightweight IDE, VSC has been loved by more and more developers, and Anaconda, as an integrated environment for Python distribution, has adopted VSC as a recommended development tool.

 

 

 

 

VSCode is lighter in magnitude, but it only needs to install a Python language support tool, and it can become a full-featured Python IDE. It has all editing functions such as intellisense, completion, refactoring, and finding and defining code segments, and the debugging function is also very good. Complete, can cover all the functions of PDB in debug mode, and can operate simply under the graphical interface.

 

If you have to talk about a weakness of VSCode, it should be that the configuration of its debugging tools and interpreter does not have a specific configuration page, which needs to be modified through the configuration file, which may cause confusion to newbies.

 

Visual Studio

 

VS is known as the first IDE in the universe, and its Python development tools can naturally bring a very good experience. In VS2017, the Python development environment is already an optional installation option. For many people who have used VS to develop other languages ​​under Windows, the familiar shortcut keys, clear environment and resource consumption that are not particularly exaggerated are definitely the plus points of VS.

 

 

 

 

Of course, its debugger function is also readily available.

 

Spyder

 

As a cross-platform IDE written in Python contributed by the open source community, Spyder is light-weight, convenient, and highly integrated. Spyder allows working in a variety of different preset modes, such as a Matlab-like scientific computing interactive interface, and other interface environments in the form of application engineering development; Spyder can prompt documents in real time, interactively run, and display during debugging during the coding process All variable tables, one-click visualization, etc., are very convenient for data analysis; similarly, it also supports a series of debugging functions provided by PDBs such as step tracking. If we talk about the shortcomings, the interface itself is not fashionable, right?

 

 

 

 

Eclipse + PyDev

 

Eclipse is one of the most brilliant open source cross-platform multilingual IDEs, with a large number of users, and naturally it also provides support for Python. PyDev is a Python development toolkit on Eclipse, which provides complete IDE functions and also includes the debugging functions such as breakpoints and stepping. Eclipse + PyDev may be the most complete open source Python IDE solution.

 

 

 

 

PyCharm

 

Maybe every Python developer is no stranger to PyCharm. As the best Python IDE at present, PyCharm maintains a quarterly update version iteration frequency, and each update can bring functional surprises, and whether you are data Analysis, application developer or server-side development, PyCharm can provide the best experience. It is the most intelligent IDE, it can infer whether you need to enter scientific computing mode through the modules you reference; it can parse other language fragments (such as SQL, HTML, JS, etc. in strings) that exist in the code; it can be combined with Jupyter Notebook Develop and display; can generate UML diagrams through code, including graphical unit testing, coverage analysis, performance analysis tools and parallel analysis tools; can remotely debug, one-click deployment, and can deeply combine various common frameworks to provide better Support; it can also be easily debugged graphically.

 

Disadvantages of PyCharm? It should be that after setting the interpreter, PyCharm will parse and extract all site-packages in the environment for automatic completion of intellisense. During this period, the IDE occupies a lot of memory and CPU, but if Your computer uses a high-speed SSD as the hard drive, and at the same time has enough memory, everything will look perfect.

 

 

 

 

Similar to Visual Studio, PyCharm is commercial software, although it also provides a free community edition, but in comparison, the community edition of PyCharm has more limited features. However, if you are still a student, you can use your education email to apply for a free Education Edition student license and experience all the same features as the Pro Edition. Forklift accessories

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325244594&siteId=291194637
Recommended