开发者

vim and python scripts debugging

Are there any ways to debug python scripts not leaving vim in *nix systems (executing the script, setting 开发者_如何学Goup breakpoints, showing variables in watch-list, etc)?


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.


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!


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


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


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


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

I do recommend it.

Hope it helps.


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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜