What is debug__ in Python

The Python Debugger

The Python module pdb provides an interactive debugger for Python programs. It includes evaluating the source code in single steps, inspecting variables, executing any Python code at any point and the so-called “post-mortem debugging”.
Start the debugger
True to the motto: “Many roads lead to Rome” - there are different ways to start the debugger. Depending on where and what is to be debugged, the user decides when the Python interpreter “enters” the debugger.
From the command line
The debugger prompt is (Pdb).
If we call the debugger from the command line, it loads our script pdb_test.py and stops at the first instruction:

$ python -m pdb pdb_test.py> ... / pdb_test.py (3)

In the interpreter
If you experiment in the interactive interpreter, it is also helpful to use the debugger here via pdb.run or pdb.runeval to be able to start:

$ python Python 2.7.2 (default, Oct 27 2011, 01:40:22) [GCC 4.6.1 20111003 (Red Hat 4.6.1-10)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import pdb >>> import pdb_test >>> pdb.run ('pdb_test.test ()')> (1) () (Pdb)

In program
In most cases, the options mentioned above are only helpful if the developed program is manageable or if problems occur at the beginning of the execution. If you come across errors in larger programs or only later in the execution, it is advantageous to start the debugger in the program:

#! / usr / bin / env python import pdb def test (): pdb.set_trace () print "Hello!" if __name__ == '__main__': test ()

The function call pdb.set_trace () in line 6 the debugger starts when the instruction is reached:

$ python pdb_test.py> ... / pdb_test.py (7) test () -> print "Hello!" (Pdb)

After a mistake
Debugging programs after a failure is called “post-mortem debugging”. pdb supports this through the functions pm () and post mortem().

class Output (object): def __init __ (self, output): self.the_output = output def echo (self): print self.output

Line 7 is hiding a AttributeErrorsince we actually self.the_output would have to spend. If the said line is called, the execution stops with the corresponding error. After that we can pdb.pm () call to start the debugger at the point at which the exception occurred:

$ python Python 2.7.2 (default, Oct 27 2011, 01:40:22) [GCC 4.6.1 20111003 (Red Hat 4.6.1-10)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import pdb_test >>> pdb_test.Output ("Hello!"). echo () Traceback (most recent call last): File "

Use debugger
The debugger understands a defined set of commands - all other statements are treated as Python code and executed in the context of the program. Even variables and functions can be changed during debugging. Entering an empty line repeats the last command entered.
navigation
w (here): Display of the position in the call stack and the next instruction to be executed:

(Pdb) where /usr/lib64/python2.7/bdb.py (387) run () -> exec cmd in globals, locals

l (is) [start [, end]]: Call without arguments shows 5 lines before and after the current line. With one argument, 5 lines are displayed before and after the specified line and with 2 arguments, the specified range is displayed:

(Pdb) list 1 #! / Usr / bin / env python 2 3 -> import pdb 4 5 class Output (object): 6 7 def __init __ (self, output): 8 self.the_output = output 9 10 def echo (self ): 11 print self.output (Pdb) l 13 8 self.the_output = output 9 10 def echo (self): 11 print self.output 12 13 if __name__ == '__main__': 14 Output ("Hello!"). echo () [EOF] (Pdb) l 5, 14 5 class Output (object): 6 7 def __init __ (self, output): 8 self.the_output = output 9 10 def echo (self): 11 print self.output 12 13 if __name__ == '__main__': 14 Output ("Hello!"). Echo ()

u (p), d (own): Current frame in the call stack upwards (up, older) or below (down, newer) move:

(Pdb) up> ... / pdb_test.py (14)

Execute step by step
s (step): Executes the current line and stops at the next function or line to be called in the current sequence:

(Pdb) ...> ... / pdb_test.py (14)

n (ext): Behaves similarly to step, but does not stop at functions that are called by the statement to be executed:

(Pdb) ...> ... / pdb_test.py (14)

return): Stops at the next return-Instruction:

#! / usr / bin / env / python import random def randint (a, b): random_int = random.randint (a, b) return random_int if __name__ == '__main__': random.seed () print randint (0, 100) (Pdb) ...> ... / pdb_return.py (11)

Check variables
a (rgs): Shows the arguments of the current function and
p expression: Evaluated expression and shows its output, for example the value of a variable:

(Pdb) ...> ... / pdb_return.py (11)

Of course, the debugger also supports Breakpoints and jump - I might write something about that in a second part.
Further information can be found in the Python documentation and the absolutely brilliant treatise by Doug Hellmann, called Python Module of the Week, whose structure I have freely adopted here.


Eric came to NETWAYS during his first year of apprenticeship and completed his apprenticeship very successfully in 2011. Since the beginning he has been working in software development and there on the various NETWAYS open source solutions, especially inGraph and in the Icinga team on Icinga Web. In addition, he is responsible for many customer developments in the finance and automotive industries.