Python has a library called pdb that
can be imported into the project and it is very straight-forward to use.
It only requires the programmer to
insert the breaking point with the code pdb.set_trace() on the desired location
to debug, and the program will enters in debugging mode when it reaches this
point of execution. Another option that is available on the more recent Python
versions (3.7) is to use the command breakpoint() that imports the pdb lib and
is similar to the pdb.set_trace().
For debugging from the command
prompt> python -m pdb debug_ex.py to enter in debug mode from the
terminal.
Some of the commands are very
similar to other commonly used debuggers in other programming languages, such
as gdb for C++. Very frequently used commands are for example: n(ext),
s(tep), c(ontinue), l(ist), p(rint) variable, q(uit), cl(ear breakpoints),
b(set a breakpoint), r(eturn), h(elp).
Here below a simple example:
debug_ex.py
import pdb
def Multiply(input_list):
output_list=[]
pdb.set_trace()
for i in range(len(input_list)):
output_list.append(input_list[i]*10)
return output_list
def main():
my_list=[1,2,3]
print(Multiply(my_list))
if __name__ == '__main__':
main()
Executing from the terminal:
$
python debug_ex.py
>
c:\...\debug_ex.py(6)Multiply()
->
for i in range(len(input_list)):
(Pdb)
n
>
c:\...\debug_ex.py(7)Multiply()
->
output_list.append(input_list[i]*10)
(Pdb)
n
>
c:\...\debug_ex.py(6)Multiply()
->
for i in range(len(input_list)):
(Pdb)
n
>
c:\...\debug_ex.py(7)Multiply()
->
output_list.append(input_list[i]*10)
(Pdb)
i
1
(Pdb)
s
>
c:\...\debug_ex.py(6)Multiply()
->
for i in range(len(input_list)):
(Pdb)
s
>
c:\...\debug_ex.py(7)Multiply()
->
output_list.append(input_list[i]*10)
(Pdb)
s
>
c:\...\debug_ex.py(6)Multiply()
->
for i in range(len(input_list)):
(Pdb)
s
>
c:\...\debug_ex.py(8)Multiply()
->
return output_list
(Pdb)
s
--Return--
>
c:\...\debug_ex.py(8)Multiply()->[10, 20, 30]
->
return output_list
(Pdb)
s
[10,
20, 30]
--Return--
No comments:
Post a Comment