Simpler way to put PDB breakpoints in Python code?

后端 未结 14 2135
不知归路
不知归路 2020-11-30 18:21

Just a convenience question. I\'ve been a bit spoiled with debuggers in IDEs like Visual Studio and XCode. I find it a bit clumsy to have to type import pdb; pdb.set_t

相关标签:
14条回答
  • 2020-11-30 19:06

    In vim, I have a macro set up for this (in my .vimrc file):

    map <silent> <leader>b oimport pdb; pdb.set_trace()<esc>
    map <silent> <leader>B Oimport pdb; pdb.set_trace()<esc>
    

    so I can just press \b (when not in Insert Mode) and it adds in a breakpoint after the current line, or \B (note the capital) and it puts one before the current line.

    which seems to work alright. Most other 'simple' programmers editors (emacs, sublimetext, etc) should have similar easy ways to do this.

    Edit: I actually have:

    au FileType python map <silent> <leader>b oimport pdb; pdb.set_trace()<esc>
    au FileType python map <silent> <leader>B Oimport pdb; pdb.set_trace()<esc>
    

    which turns it on only for python source files. You could very easily add similar lines for javascript or whatever other languages you use.

    2019 Update (Python 3.7+)

    Python 3.7+ now has the builtin breakpoint() which can replace the previous import pdb; pdb.set_trace() in vim. It still works the same.

    0 讨论(0)
  • 2020-11-30 19:06

    A underrated method is to set breakpoint in pdb directly:

    pdb> b torch/__init__:10
    

    will set a breakpoint on site-packages\torch\__init__.py:10

    Then pdb> c will stop on this breakpoint.

    Following are valid too:

    pdb> b d:\anaconda\lib\site-packages\torch\__init__.py:10
    pdb> b torch\__init__.py:10
    pdb> b d:\\anaconda\\lib\\site-packages\\torch\\__init__.py:10
    pdb> b d:/anaconda/lib/site-packages/torch/__init__.py:10
    

    See doc for detail.

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