


Everyone knows that Python comes with its own Pdb library, and it is very convenient to use Pdb to debug Python programs. However, remote debugging and multi-threading cannot be handled by Pdb. Let’s take a look at how to use the Pdb library to debug Python and commonly used commands.
There are many ways to use Pdb to debug
The main ways to use Pdb to debug Python programs are the following three! Let’s introduce one by one
Command line plus -m parameter
Command line to start the target program, add -m parameter, so that the breakpoint is called when testPdb.py is called It is before the first line of program execution
The example display that will be focused on in this article is debugging in this way!
python -m pdb testPdb.py
Debugging in python interactive environment
>>> import pdb >>> import testPdb >>> pdb.run('testPdb.test()')
Insert a program into the code
It is more commonly used to insert a program in the middle of the program. Compared with setting a breakpoint in a general IDE and then starting debugging, this method The method is hardcode
if __name__ == "__main__": a = 1 import pdb pdb.set_trace() b = 2 c = a + b print(c)
and then run the script normally: python testPdb.py
to pdb. set_trace()
It will be settled there, and then you can see the debugging prompt (Pdb)
The debugging situation for the above small program is as follows:
Prepare the test program
Next, use the first method introduced above to debug the Python program to introduce the commonly used commands of pdb, but at the beginning Beforehand, you must prepare the test program code:
testFun.py
This is a submodule that will be called by the main module for testing. When debugging Pdb, is it possible to trace breakpoints from the main module into the submodule (explanation will follow)
#!/usr/bin/python # -*- coding: utf-8 -*- def add(a, b): return a + b
testPdb.py
This is the code of the main module being debugged below
#!/usr/bin/python # -*- coding: utf-8 -*- def sub(a, b): return a - b if __name__ == "__main__": print '' import testFun i = 0 a = 1 while(i < 100): a = testFun.add(a, 1) i = i + 1 print "累加结果:", a print "" for letter in 'Pdb': print "当前字母:", letter print "" fruits = ['banana', 'apple', 'mango'] for fruit in fruits: print "当前水果:", fruit print "" ret = 0 for num in range(10, 12): ret = sub(ret, num) print '循环结果:', ret print "" d = {'abc': 123, 123: "abc"} for (k,v) in d.items(): print "当前键值对:", k, '-', v print ""
Summary of commonly used commands
Basic commands
h(elp) command: will print the commands available in the current version of Pdb. If you want to query a Command, you can enter h [command]
, for example h l
View list command
l(ist) command: You can list the code blocks currently to be run
Breakpoint management
b(reak): Set breakpoint
For example, b 12 is to add a breakpoint on line 9 of the current script
For example, b sub is to add a breakpoint at the sub function definition of the current script.
In addition to adding breakpoints in the current script, you can also break other scripts in the current script. Click, take the code used above as an example b testFun.add
You can add a breakpoint at the add function in the testFun.py script
If you only use b All existing breakpoints will be displayed
condition bpnumber [condition]: Set a conditional breakpoint, such as condition 2 a==0, which means adding the condition "a==" to the second breakpoint 0”
cl(ear): Delete the breakpoint. If there are parameters behind it, it will be the specified breakpoint; if there are no parameters, it will clear all breakpoints
disable/enable: Disable/activate breakpoints
Program logic control
The commands shown below require you to know the code and line number of the corresponding script, so here we first The screenshot shows the first few lines of code required for the test below
c(ont(inue)), allowing the program to run normally until it encounters the next breakpoint
n(ext), allowing The program runs the next line. If the current statement has a function call, using n will not enter the called function body.
As shown in the figure below, when debugging the script breakpoint to testFun. When add(a, 1)
, continue to execute n and will not enter the function inside testFun.add(a, 1)
s(tep), similar to n , but if there is currently a function call, then s will enter the called function body
As shown in the figure below, when debugging the script breakpoint to testFun.add(a, 1)# When ##, continue to execute s, you will enter the function definition corresponding to
testFun.add(a, 1), although
testFun.add is not a function defined in this script
Print important information
testFun.add, the parameters
p of
testFun.add
## is printed.
#Exit debugging
q, exit debugging directly; or use Ctrl+D to exit
Summary
The process of debugging using Pdb shown above is actually very simple. The article mainly shows the running effect through screenshots. If you simply read the article, you will be very clueless, and you may even feel that the commands and output in the screenshots are messy. However, if you follow the process yourself, it will not take an hour, but the effect will be absolutely excellent! One more thing, Python's debugger is Pdb, which can be used to learn the C debugger gdb under Linux. The above is the entire content of this article. I hope it will be helpful to everyone's study and work.
For more articles on how to use the Pdb library to debug Python and commonly used commands, please pay attention to the PHP Chinese website!

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python and C have significant differences in memory management and control. 1. Python uses automatic memory management, based on reference counting and garbage collection, simplifying the work of programmers. 2.C requires manual management of memory, providing more control but increasing complexity and error risk. Which language to choose should be based on project requirements and team technology stack.

Python's applications in scientific computing include data analysis, machine learning, numerical simulation and visualization. 1.Numpy provides efficient multi-dimensional arrays and mathematical functions. 2. SciPy extends Numpy functionality and provides optimization and linear algebra tools. 3. Pandas is used for data processing and analysis. 4.Matplotlib is used to generate various graphs and visual results.

Whether to choose Python or C depends on project requirements: 1) Python is suitable for rapid development, data science, and scripting because of its concise syntax and rich libraries; 2) C is suitable for scenarios that require high performance and underlying control, such as system programming and game development, because of its compilation and manual memory management.

Python is widely used in data science and machine learning, mainly relying on its simplicity and a powerful library ecosystem. 1) Pandas is used for data processing and analysis, 2) Numpy provides efficient numerical calculations, and 3) Scikit-learn is used for machine learning model construction and optimization, these libraries make Python an ideal tool for data science and machine learning.

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver Mac version
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft