Stepwise debugging of selected Python code

末鹿安然 提交于 2019-11-30 22:08:38

(Spyder maintainer here) There is a trick not many people know to introduce breakpoints wherever you want in your Python code: import pdb; pdb.set_trace().

In your case, you need to replace your code with

def foo():
    names = ['A', 'B', 'C']
    values = [11,12,13]
    i = 0
    import pdb; pdb.set_trace()
    for n in names:
        variable = str(n)  + ' = ' + str(values[i])
        print(variable)
        i += 1

foo()

and then run your file with Run file/F5 or also as a cell with Ctrl + Enter. With this simple change you'll get a debugger session exactly at the line pdb.set_trace() is placed and enter the for cycle below it to iterate line by line through it.

Note: Since Python 3.7 this is even simpler because it adds a new builtin function called breakpoint to replace pdb.set_trace(), but with the exact same effect.

Visual Studio Community Edition

(with some Add-Ins not relevat to python):

Disclaimer: I use it because im familiar with it (C#) and it is free to use - not because it's especially pythonic.


Coding (Layout configureable):

  • Left: Code

  • Left-low: Output (normally I collaps that down to tabs only at the bottom to get more coding screen estate. Maybe downsized and switched to tab Error List which lists errors in your code (doh). Your code was perfect, so you do not see any of the sqigglies pointing out errors in the code.

  • Right: Solution explorer - essentially what files are inside this special python solution.

  • Right-low: Properties of a selected file

All the windows can be removed, resized, un-pinned to become "free floating windows" or collapsed into vertical tabs at left/right side of the window to get more screen estate for whats important to you)

See the red dots on the side of the lines? You place them before running the code. They mark break points, code execution automatically stops running when hitting one (basic use) or you can configure them to

  • not stop running but print out trace info of variables
  • stop only conditionally on f.e. variable value becomes 5 (loop index after that your program crashes all the time or other "conditions")
  • they can also only stop on 4th time hit and some other things:


Debugging Code:

F5 to start code running changes the layout (see above, you can change it and changes persist for next run):

When stopped you can hover over variables that are already used and inspect them, you can pin those information to the side - if one changes the text of the pin will get red to visualize this.

You can also put stuff in the "watch window" or use "quick watch" to inspect it - window lower half of screenshot. From a break you can continue wiht F5 to run till next breakpoint, or use F10 or F11:

  • F10 "steps over" statements line by line
  • F11 "steps into" statements - i.e. if your statement is function call it would execute the function wiht F10 and step into the function with F11

If inside a function you can use SHIFT+F11 complete the function and step out again and go from there.

I used F5 and F10 twice and I am currently at the line indicated by the arrow-thingy. You can also dragg the arrow some lines up to reexecute code, but this can have sideeffects - I rarly use it, most times I just restart the program Ctrl+Shift+F5 or stop debugging Shift+F5

The print output of your program is shown in a normal Console window thats cropped from the screenshots.

AddOn1: You can also simply execute all code, no debugging with CTRL+F5 and the IDE has 'a few' other features for python.

AddOn2: VS naturally comes with a lot of other features useful in an IDE, like integration to git etc. - if you use those they are "usable" from within the IDE via plugins -although some things are better done on the git.bash.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!