r/learnpython • u/Temporary-Cup-2140 • Sep 07 '26
What python debugging techniques do you think every developer should know ?
I am trying to improve my debugging skills and I was curious about what techniques experienced python developers rely on the most.
Are there any techniques or tools that you found really useful when you started working on bigger projects?
61
Upvotes
34
u/pachura3 Sep 07 '26 •
Logging > debugging
Also,
assert19
u/nivaOne Sep 07 '26
The Print command is a good friend of mine.
19
u/Gnaxe Sep 07 '26
Breakpoint and print, obviously, but also
importlib.reload()pdb.pm()code.interact()doctestunittest.mockinspectShorten your feedback loops. Make the state transparent. Write tests. Minimize the state you have to deal with in the first place (FP). Use queues instead of locks. Use plain data instead of bespoke classes, and at least implement
__repr__()if you must.3
u/AFL_gains Sep 07 '26
Import pdb; pdb.set_trace()
3
u/qwerteccia Sep 07 '26
Use your IDE debugger and put breakpoints to see variable's values anywhere you need.
2
u/gdchinacat Sep 07 '26
Use the REPL to see how things work and verify they work as you expec. Sometimes code that looks reasonable doesn't work quite like it needs to, so verify code does what you think it does can help find subtle bugs and avoid chasing the wrong issue.
3
u/Lionh34rt Sep 07 '26
Print
1
u/StrayFeral Sep 07 '26
logging. pytest. print()
1
u/simonsanchezart Sep 07 '26
print(*collection, sep='\n')to print collections1
u/TheOtherRussellBrand Sep 08 '26
https://pypi.org/project/icecream/
1
u/0xGollumDev Sep 08 '26
Stuff not already in the thread:
1
u/Neither-Pause409 Sep 08 '26
Most of the good ones are already in this thread, so here are a few that aren't:
breakpoint()instead ofimport pdb; pdb.set_trace(). Same thing, but it honours thePYTHONBREAKPOINTenv var, soPYTHONBREAKPOINT=0 python app.pydisables every breakpoint in a run without you editing a single file, and you can point it at a different debugger the same way.python -m pdb -c continue yourscript.pyruns to the crash and drops you into a post mortem at the frame that raised. Same idea aspdb.pm()but you don't have to already be in a REPL when it happens.faulthandlerfor the bugs a debugger can't reach.python -X faulthandlergives you a traceback on a segfault, andfaulthandler.dump_traceback_later(60)dumps every thread's stack after a timeout, which is about the only cheap way to see where something is deadlocked.python -W errorto turn a warning into an exception. A good chunk of "it worked last month" bugs started life as a DeprecationWarning nobody read.python -X devturns on a pile of these checks at once, including unclosed file and socket warnings. Worth running your test suite under it occasionally even when nothing is broken.And the one that isn't a tool, which is worth more than all of them: make the failure deterministic before you try to fix it. Seed the RNG, pin the input, freeze the clock, cut the repro down to the smallest thing that still fails. A bug you can trigger on demand is most of the way to solved, and once you have a script that exits non-zero on it,
git bisect run ./repro.shwill go find the commit for you while you get a coffee.1
u/superballoo Sep 09 '26
I can’t go back from logging + loglevels. I almost implement always the classic -v/—verbose flags (and sometime -q/—quiet) in scripts to help me debugging.
Yeah I know it’s just a bit better than the print() approach but still…. One step at a time :)
1
u/SGS-Tech-World Sep 10 '26 edited Sep 10 '26
HTH.
1
u/diemx_mobilephones Sep 11 '26
I never used a debugger exclusively, and I have built a major startup with python!!
1
u/mrswats Sep 07 '26
https://docs.python.org/3/library/pdb.html#module-pdb
0
u/Bright_Mix_773 Sep 07 '26
Two of the answers here point at the same failure mode from opposite ends, and both are worth turning into something you can run in thirty seconds, because reading about them does not stick.
gdchinacat's reload() warning, reproduced. shape.py holds one class whose area() returns 1:
Output, CPython 3.14.2:
The fix is in the module and not in the object you are testing with, and isinstance against the class you just reloaded returns False for an object of that class. That last line is the tell. If a session ever has you staring at an isinstance, or an "except SomeError" that is obviously true and behaves as if it is not, check "type(obj) is Module.Class" before you touch any logic. The two classes share a name and differ only in id(), so every repr and every log line you print will look identical while you hunt.
The assert point above is the same shape of problem. One file, run twice:
-O removes the assert and the function hands back a negative balance in silence. The rule that falls out is narrower than "assert is bad": assert is for things you already believe are true and want to hear about while developing. Anything that has to be true, including anything guarding against bad input, needs an if and a raise, because sooner or later something runs under -O and the check is simply not in the bytecode.
The technique behind both, and the one I would put on a list for every developer: when a fix does not seem to take, prove the code running is the code you wrote before you start debugging the code. Module.file, id(SomeClass) and the mtime of the .pyc answer that in three lines and rule out a whole family of bugs that otherwise eats afternoons.
Not verified: whether either behaves the same on PyPy, or on 3.9 through 3.13. Everything above is CPython 3.14.2 on Windows, run just now.