varnie
varnie

Reputation: 2595

vim and python scripts debugging

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

Upvotes: 45

Views: 42169

Answers (10)

Santosh Kumar
Santosh Kumar

Reputation: 27875

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

Upvotes: 8

Tinmarino
Tinmarino

Reputation: 4031

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

Upvotes: 4

user5554773
user5554773

Reputation:

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

I do recommend it.

Hope it helps.

Upvotes: 2

oleckkxs
oleckkxs

Reputation: 1310

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

Upvotes: 7

Viktor Vojnovski
Viktor Vojnovski

Reputation: 1371

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.

Upvotes: 1

Pierre-Antoine LaFayette
Pierre-Antoine LaFayette

Reputation: 24402

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.

Upvotes: 52

ashim
ashim

Reputation: 25560

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!

Upvotes: 7

zeroDivisible
zeroDivisible

Reputation: 4130

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.

Upvotes: -4

Walter
Walter

Reputation: 7981

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.

Upvotes: 0

pavium
pavium

Reputation: 15118

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.

Upvotes: -1

Related Questions