Starting python debugger automatically on error

前端 未结 13 2298
南方客
南方客 2020-11-27 08:44

This is a question I have wondered about for quite some time, yet I have never found a suitable solution. If I run a script and I come across, let\'s say an IndexError, pyth

13条回答
  •  无人及你
    2020-11-27 09:24

    This isn't the debugger, but probably just as useful(?)

    I know I heard Guido mention this in a speech somewhere.

    I just checked python -?, and if you use the -i command you can interact where your script stopped.

    So given this script:

    testlist = [1,2,3,4,5, 0]
    
    prev_i = None
    for i in testlist:
        if not prev_i:
            prev_i = i
        else:
            result = prev_i/i
    

    You can get this output!

    PS D:\> python -i debugtest.py
    Traceback (most recent call last):
      File "debugtest.py", line 10, in 
        result = prev_i/i
    ZeroDivisionError: integer division or modulo by zero
    >>>
    >>>
    >>> prev_i
    1
    >>> i
    0
    >>>
    

    To be honest I haven't used this, but I should be, seems very useful.

提交回复
热议问题