Is there a way to step through a program?

I don’t understand how my program is failing.

To run only part of the code, you can comment out the rest:

#print('a')
print('b')

It should output only b.

Debugger might be useful pdb — The Python Debugger — Python 3.12.4 documentation.

Or you can use IDE that has debugger integrated visually, such as VSCode or PyCharm.

IDLE also has a visual debugger that steps through code.

Where do I find out about IDLE?

“Assuming you already have the script open in IDLE, open a Python shell window (Run->Python Shell), click Debug->Debugger in that window, and then run the script.”

This tutorial seems to be ok from first glance: Getting Started With Python IDLE – Real Python

You can do a bit more search on google if you don’t like this one, I am sure there are many more.

If you install tkinter, you should have IDLE.

Thanks! …

Yes. Run your program with python -m pdb program.py. When you run the program like this you get a debugger prompt.

  1. Set a breakpoint in line 19: b 19
  2. Use n to execute one line at a time.
  3. Use c to continue until the next breakpoint, or to the end of the program.
  4. Use quit() to quit the debugger.
  5. Use clear 1 to clear breakpoint 1. In Python the breakpoints are numbered and you use that breakpoint number, not the line number, to clear the breakpoint.
  6. Use p myvar to view a variable value, even lists or dicts.
  7. Make a plain text file called .pdbrc to make an alias for the debugger just for that program. Use # to begin a comment. Use alias q quit() to make an alias.
  8. This alias displays a list of files while in the debugger, uses 2 statements, and even takes a parameter: alias dir import glob; print(glob.glob("%1"))

pdb is just one debugger you can use with Python, there are others, but I’m not familiar with them.

1 Like