Step-by-step debugging with IPython

后端 未结 15 2074
隐瞒了意图╮
隐瞒了意图╮ 2020-12-02 03:43

From what I have read, there are two ways to debug code in Python:

  • With a traditional debugger such as pdb or ipdb. This supports c

相关标签:
15条回答
  • 2020-12-02 03:53

    Prefixing an "!" symbol to commands you type in pdb seems to have the same effect as doing something in an IPython shell. This works for accessing help for a certain function, or even variable names. Maybe this will help you to some extent. For example,

    ipdb> help(numpy.transpose)
    *** No help on (numpy.transpose)
    

    But !help(numpy.transpose) will give you the expected help page on numpy.transpose. Similarly for variable names, say you have a variable l, typing "l" in pdb lists the code, but !l prints the value of l.

    0 讨论(0)
  • 2020-12-02 03:54

    From python 3.2, you have the interact command, which gives you access to the full python/ipython command space.

    0 讨论(0)
  • 2020-12-02 03:55

    Developing New Code

    Debugging inside IPython

    1. Use Jupyter/IPython cell execution to speed up experiment iterations
    2. Use %%debug for step through

    Cell Example:

    %%debug
    ...: for n in range(4):
    ...:    n>2
    

    Debugging Existing Code

    IPython inside debugging

    1. Debugging a broken unit test: pytest ... --pdbcls=IPython.terminal.debugger:TerminalPdb --pdb
    2. Debugging outside of test case: breakpoint(), python -m ipdb, etc.
    3. IPython.embed() for full IPython functionality where needed while in the debugger

    Thoughts on Python

    I agree with the OP that many things MATLAB does nicely Python still does not have and really should since just about everything in the language favors development speed over production speed. Maybe someday I will contribute more than trivial bug fixes to CPython.

    https://github.com/ipython/ipython/commit/f042f3fea7560afcb518a1940daa46a72fbcfa68

    See also Is it possible to run commands in IPython with debugging?

    0 讨论(0)
  • 2020-12-02 03:56

    What about ipdb.set_trace() ? In your code :

    import ipdb; ipdb.set_trace()

    update: now in Python 3.7, we can write breakpoint(). It works the same, but it also obeys to the PYTHONBREAKPOINT environment variable. This feature comes from this PEP.

    This allows for full inspection of your code, and you have access to commands such as c (continue), n (execute next line), s (step into the method at point) and so on.

    See the ipdb repo and a list of commands. IPython is now called (edit: part of) Jupyter.


    ps: note that an ipdb command takes precedence over python code. So in order to write list(foo) you'd need print(list(foo)), or !list(foo) .

    Also, if you like the ipython prompt (its emacs and vim modes, history, completions,…) it's easy to get the same for your project since it's based on the python prompt toolkit.

    0 讨论(0)
  • 2020-12-02 03:57

    The Pyzo IDE has similar capabilities as the OP asked for. You don't have to start in debug mode. Similarly to MATLAB, the commands are executed in the shell. When you set up a break-point in some source code line, the IDE stops the execution there and you can debug and issue regular IPython commands as well.

    It does seem however that step-into doesn't (yet?) work well (i.e. stopping in one line and then stepping into another function) unless you set up another break-point.

    Still, coming from MATLAB, this seems the best solution I've found.

    0 讨论(0)
  • 2020-12-02 04:01

    You can use IPython's %pdb magic. Just call %pdb in IPython and when an error occurs, you're automatically dropped to ipdb. While you don't have the stepping immediately, you're in ipdb afterwards.

    This makes debugging individual functions easy, as you can just load a file with %load and then run a function. You could force an error with an assert at the right position.

    %pdb is a line magic. Call it as %pdb on, %pdb 1, %pdb off or %pdb 0. If called without argument it works as a toggle.

    0 讨论(0)
提交回复
热议问题