Search code examples
pythondebuggingipythonpdbipdb

iPython debugger not providing any insight


Let's say I have the following function:

def simple_func():
    a = 4
    b = 5
    c = a * b
    print c

Here's what I get when I run %debug simple_func():

NOTE: Enter 'c' at the ipdb>  prompt to continue execution.
None
> <string>(1)<module>()

ipdb>

If I enter n the debugger spits 20 back out at me and returns None.

This is a simplified version of what's happening across functions, interpreters, machines, etc. What's going on? Why can't I get any of my debuggers to do what I want, when all I need is to do some very simple line-by-line stepping through?


Solution

  • It doesn't look like debug works with a function that's simply defined in the ipython session. It needs to be imported from a file (that is, the --breakpoint parameter takes a file name and line).

    If I create a file test.py

    In [9]: cat test.py
    def simple_func():
        a = 4
        b = 5
        c = a * b
        print(c)
    

    I can do:

    In [10]: import test
    
    In [11]: %debug --breakpoint test.py:1 test.simple_func()
    Breakpoint 1 at /home/paul/mypy/test.py:1
    NOTE: Enter 'c' at the ipdb>  prompt to continue execution.
    > /home/paul/mypy/test.py(2)simple_func()
    1     1 def simple_func():
    ----> 2     a = 4
          3     b = 5
          4     c = a * b
          5     print(c)
    
    ipdb> n
    > /home/paul/mypy/test.py(3)simple_func()
    1     1 def simple_func():
          2     a = 4
    ----> 3     b = 5
          4     c = a * b
          5     print(c)
    
    ipdb> n
    > /home/paul/mypy/test.py(4)simple_func()
          2     a = 4
          3     b = 5
    ----> 4     c = a * b
          5     print(c)
          6 
    
    ipdb> a,b
    (4, 5)
    ipdb> n
    > /home/paul/mypy/test.py(5)simple_func()
          2     a = 4
          3     b = 5
          4     c = a * b
    ----> 5     print(c)
          6 
    
    ipdb> c
    20
    ipdb> c
    20
    ipdb> q
    

    There may be other ways of using this, but this seems to be simplest, most straight forward one. I rarely use the debugger. Instead I test code snippets interactively in Ipython, and sprinkle my scripts with debugging prints.