In Python there are some libraries to debug code cProfile
where you can follow the execution trace of a program, for example:
import cProfile, pstats, StringIO
pr = cProfile.Profile()
pr.enable()
# Nuestro código
result = [i for i in range(10)]
print(result)
pr.disable()
s = StringIO.StringIO()
sortby = 'cumulative'
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
ps.print_stats()
print s.getvalue()
It would give us as a result:
2 function calls in 0.000 seconds
Ordered by: cumulative time
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 {range}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
But I would like to know, what libraries or tools exist to follow the execution flow of a complex program where there are hundreds or thousands of executions in a simpler way?
This can be directly linked to personal options, I am going to mention the debugger that comes by default in Python and at the end I will leave you the list of other debuggers so that you can try them and choose the one that suits you best.
python debugger
Python has its own Debugger called
pdb
. You use it as you would use breakpoints within your code, that is, you can use it where you need it and as many times as you want.The most common way of use is through the
pdb.set_trace()
. Consider the following example script:When running it:
You will see something like this:
When
(Pdb)
you see it, it tells you that the execution of the program has stopped and is waiting for some debugging command:The line before
(Pdb)
tells you the line to be executed next:Which is equivalent to line 4 of your script. Before continuing, you can see the available commands by pressing
h
or?
:We are not going to go into detail about all the commands that can be used, just some basic ones.
n
For example, the o commandnext
allows you to execute the following line of code, which in this case would beelemento **= 2
:The line has just been executed
elemento **= 2
and, therefore, we could have access to its value, also to the value of the list that was initially defined, in fact, to any variable that has been defined up to that moment:The command
n
will continue on the next line even if it contains a function. Now consider the following script that uses a function to return the power of a number:When running it:
Note that the next line to be executed is the function call
potencia
, if we usen
it it will execute the function and return to the next line which is the next iteration of thefor
:But what if we want to know what the function executes
potencia
? For that we can use thes
o commandstep
, this command will continue inside the first line of the function that has been called. If we run again:c
Then we have the o commandcontinue
, which, as its name says, continues with the execution of the program, but since we are inside afor
it will continue until it finishes iterating each element. Therefore:q
To finish, I think it is also necessary to know the o commandquit
that ends the execution of the debugger and the program is aborted:For more detail on debugging commands you can see the Debugger Commands section .
Python Debugger (with autocomplete)
In fact, it
pdb
recognizes when you press Tab, but it just shows you the list of commands. Consider the following script:When executing:
It doesn't work to view the methods of the integer variable
var_a
. There is a little trick using therlcompleter
. Modified version of the script:When executing:
Well, that already helps the debugging task a little more.
Other tools:
You can check the list of Python Debuggers on the official Python page:
PuDB has a good reputation:
https://pypi.python.org/pypi/pudb
I hope it is what you need.