vim and python scripts debugging

Question:

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)?

Asked By: varnie

||

Answers:

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.

Answered By: pavium

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.

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.

Answered By: zeroDivisible

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.

Answered By: Walter

Try pyclewn. It allows to use vim as front end for pdb. You can create/delete break points, control flow of debugging process, look at values of your variables. All from vim!

Answered By: ashim

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.

Answered By: Viktor Vojnovski

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

Answered By: alexche8

The vimpdb plugin integrates the Python debugger pdb into the VIM editor.

I do recommend it.

Hope it helps.

Answered By: simone

As 2020 the Debugger Adapter Protocol is taken care by vimspector.
Supporting Cpp, Python, Java, Js, Go …
See my other answer

Answered By: Tinmarino

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

Answered By: Santosh Kumar
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.