Are there any ways to debug python scripts not leaving vim in *nix systems (executing the script, setting up breakpoints, showing variables in watch-list, etc)?
10 Answers
Use pdb:
import pdb
def main():
list = [1,2,3]
pdb.set_trace()
list = [2,3,4]
if __name__ == '__main__':
main()
Now run using :!python %
and you'll hit your breakpoint and be able to debug interactively like in gdb.

- 10,763
- 7
- 51
- 69

- 24,222
- 8
- 54
- 58
-
2theres also ipdb which is a little ipython like, so more user friendly. – michael Dec 14 '09 at 22:27
-
I'm getting [this error](http://stackoverflow.com/questions/34914704/bdbquit-raised-when-debugging-python) – Iulian Onofrei Dec 28 '16 at 23:14
As of Python 3.7, you can use breakpoint()
builtin without importing anything.
Built-in breakpoint()
calls sys.breakpointhook()
. By default, the latter imports pdb
and then calls pdb.set_trace()
Inheriting code from Pierre-Antoine's answer, the code would look like this:
def main():
list = [1,2,3]
breakpoint()
list = [2,3,4]
if __name__ == '__main__':
main()
Source: https://docs.python.org/3/whatsnew/3.7.html#pep-553-built-in-breakpoint

- 26,475
- 20
- 67
- 118
Also try https://pypi.python.org/pypi/pudb - its like pdb but more advanced. Contains code highlighting, stack, showing avaliable values, etc. Not only-vim solution but for me works perfectly.
Three Steps:
Install:
pip install pudb
Paste set_trace in code
from pudb import set_trace; set_trace()
Run your code

- 1,270
- 3
- 19
- 26
As 2020 the Debugger Adapter Protocol is taken care by vimspector. Supporting Cpp, Python, Java, Js, Go ... See my other answer

- 3,693
- 24
- 33
Vim and pdb-clone is the combination I use. I use Home - pyclewn which provides a replacement for pdb called pdb-clone that is quite faster than vanilla pdb. It integrates well with vim via a plugin, and the thing I appreciate most is that it takes care of breakpoints outside the code, not setting traces within, thus not messing up my line numbers. It does not have a watch window for python yet. You might have a look at vim-debug too, which I could not get to work with my existing highlighting setup.

- 1,191
- 1
- 7
- 19
See the "Debugging" section in this blog post. It shows how to setup F7 to set breakpoints and Shift+F7 to remove breakpoints. It also uses pdb
, as mentioned before. With a little modification, you can replace the use of pdb
with ipdb
(pdb
using ipython), which is a lot nicer to use.

- 7,809
- 1
- 30
- 30
It sounds like you want to use VIM as a Python IDE.
A quick Google search found this and this example, with many more.
EDIT: Well, Ok, it seems likely you've searched more than I have.
I hope someone else has some ideas.

- 14,808
- 4
- 33
- 50
-
I've already read this article. The VimPDB mentioned there didn't work for me and gets broken each time i tried to use it. – varnie Dec 14 '09 at 13:14
From what I know, there is one more option: You could use Eclipse + PyDev for project managing and Vim as an editor for Eclipse. That way You could use the best of both worlds.
Also, I haven't tried it, but You could try this script.

- 4,041
- 8
- 40
- 62