Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IPython help functionality in ipdb debugger

Help is available in a standard IPython shell via the help command or by using the ? character. For example, for help on the built-in sum function, either of the below commands in an IPython shell could be used.

In [1]: help(sum)
Help on built-in function sum in module builtin:
...

In [2]: sum?
Signature: sum(iterable, start=0, /)
Docstring: ...

I want to have the same functionality in an ipdb debugger. One enters an ipdb debugger by placing the below code at the location for a debug breakpoint.

from ipdb import set_trace
set_trace()

However, once inside the ipdb debugger the Help functions no longer work.

ipdb> help(sum)
*** No help for '(sum)'
ipdb> sum?
*** SyntaxError: invalid syntax
ipdb>

Help in IPython shell and ipdb debugger

The below command represents a way to print a docstring inside the ipdb debugger, however this is not exactly the same functionality as help(sum) and sum? in the IPython shell.

ipdb> print(sum.__doc__)

How then do I get the same help functionality in an ipdb debugger that exists in the IPython shell?

like image 723
Russell Burdt Avatar asked Nov 07 '16 23:11

Russell Burdt


People also ask

How do I use IPython for debugging?

IPython has another way to start a debugger. You don't need to modify the source code of any file as we did before. If you run the %run -d filename.py magic command, IPython will execute the filename.py file and put a breakpoint on the first line there. It's just as if you would put the import ipdb; ipdb.

How do you set a breakpoint in Python pdb?

It's easy to set a breakpoint in Python code to i.e. inspect the contents of variables at a given line. Add import pdb; pdb. set_trace() at the corresponding line in the Python code and execute it. The execution will stop at the breakpoint.

How do you debug a notebook in Jupyter?

Debug code in Jupyter notebooksSet the breakpoints in the selected cell and press Alt + Shift + Enter for Windows or ⌥⇧↩ for macOS. Alternatively, you can right-click the cell and select Debug Cell from the context menu. The Jupyter Notebook Debugger tool window opens.


1 Answers

It looks like you can preface it with a ! (which is short for exec)

ipdb> !help(sum)
Help on built-in function sum in module builtins:

sum(iterable, start=0, /)
    Return the sum of a 'start' value (default: 0) plus an iterable of numbers

    When the iterable is empty, return the start value.
    This function is intended specifically for use with numeric values and may
    reject non-numeric types.
(END)
like image 95
Wayne Werner Avatar answered Oct 19 '22 05:10

Wayne Werner