Step-by-step debugging with IPython

后端 未结 15 2239
隐瞒了意图╮
隐瞒了意图╮ 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: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.

提交回复
热议问题