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.
- Set a breakpoint in line 19:
b 19 - Use
nto execute one line at a time. - Use
cto continue until the next breakpoint, or to the end of the program. - Use
quit()to quit the debugger. - Use
clear 1to clear breakpoint 1. In Python the breakpoints are numbered and you use that breakpoint number, not the line number, to clear the breakpoint. - Use
p myvarto view a variable value, even lists or dicts. - Make a plain text file called
.pdbrcto make an alias for the debugger just for that program. Use#to begin a comment. Usealias q quit()to make an alias. - 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.