Question

I am trying to debug a python code using pdb. I have a variable that called c and when I press c to print this variable the pdb get confused and continue debugging to the next break point. How can I avoid such confusion given that it would be very difficult to change the name of the variable.

Était-ce utile?

La solution 3

Your confusion is about what the various commands in PDB do. I think of it a bit like a MUD and that works fairly often:

Use p to print out the contents of a variable (or pp to pretty-print (or handle your character's basic needs)):

(Pdb) p df
Empty DataFrame
Columns: [Dist, type, Count]
Index: []

Type where or w to see where you are on the stack:

(Pdb) w
-> return df[df['type']=='dev'][['Dist','Count']].as_matrix()
  /home/user/core/ops.py(603)wrapper()
-> res = na_op(values, other)
> /home/user/core/ops.py(567)na_op()
-> raise TypeError("invalid type comparison")

See that little > arrow? That's where we are in the stack.

Use list or l to look around:

(Pdb) list
564               try:
565                   result = getattr(x, name)(y)
566                   if result is NotImplemented:
567  >>                     raise TypeError("invalid type comparison")
568               except (AttributeError):
569  ->                 result = op(x, y)
570   
571           return result
572   
573       def wrapper(self, other):
574           if isinstance(other, pd.Series):

To move around in the stack continue MUDing and use up (u) or down (d).

Use args (a) to examine what arguments the current function was called with:

(Pdb) args
dat = array([], shape=(0, 3), dtype=float64)
dev_classes = {81, 82, 21, 22, 23, 24, 31}

Use interact to enter the code at the current point in the stack. Ctrl+D brings you back in to PDB.

Autres conseils

You can tell pdb not to evaluate things like that using the ! prefix:

 >>> !c
 ... <value of c>

To print a variable, use p

p c

will print the value of the variable c

e.g:

>>> import pdb
>>> c = [1,2,3]
>>> pdb.set_trace()
--Return--
> <stdin>(1)<module>()->None
(Pdb) p c
[1, 2, 3]
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top