Python program debugging is used by many programmers. The following describes Python-related knowledge in detail. Hope to help you. Python program debugging can also implement a single-step debugging function similar to c/c ++, while the debugging module PDB in python is similar to the Common commands of GDB in c) for convenient debugging.
The following is an example (debug_demo.py ):
- #! /Usr/bin/env python
- _ Debug = True
- Def debug_demo (val ):
- If _ debug:
- Import pdb
- Pdb. set_trace () # introduce related pdb modules
- If val <10:
- Print "less than 10"
- Elif val <20:
- Print "less than 20, but big than 10"
- Else:
- Print "other condition"
Perform debugging in the command line as follows:
- python debug_demo.py
In this case, you will enter the gdb-like interface to start related debugging.
- /home/tower/test/python/debug.py(9)debug_demo()
- -> if val <= 10:
- (Pdb)
You can enter h to view available command sets, which is similar to gdb ). Single-step debugging is very helpful in some situations.
Single-step debugging makes sense for the logic tracking of key parts of the program. Therefore, PDB is similar to GDB in C/C ++ in python. however, compared with GDB, PDB functions are much weaker, so it is impossible to use all the feature of GDB in PDB. it is expected that the PDB will be improved as python is upgraded to version 3.0 and later. it is worth looking forward.