Question

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

 45  41573  45
1 Jan 1970

Solution

 51

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.

2009-12-14

Solution

 8

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

2020-09-17