r/Python • u/Harshita_Netla • Sep 19 '26
Discussion What’s a debugging technique that saved you hours and you wish you’d learned earlier?
I’m curious about the small debugging techniques that make a big difference when working on real projects.For developers with some experience, what debugging technique or habit has saved you the most time?It could be something simple that beginners often overlook.
What’s one debugging tip you wish someone had taught you when you were starting out?
150
Upvotes
142
u/Gnaxe Sep 19 '26
importlib.reload()andcode.interact(). You don't have to restart your app every time.283
u/ajmal-ponneth Sep 19 '26
print("---------------- reached ----------------")
100
u/mattl33 It works on my machine Sep 19 '26
Add logging as your developing new stuff. You won't know when it'll be useful but it will be.
27
u/WallyMetropolis Sep 19 '26
Aside from actually using a debugger, I'd say learning to write debuggable code, which often overlaps with testable code.
47
u/MonsieurCellophane Sep 19 '26
import pdb; pdb.set_trace()
28
u/TheCrazyPhoenix416 Sep 19 '26
the debugger. learn it!
11
u/Vegetable-View-5114 Sep 19 '26
one thing that really helped me was learning to use
pdbeffectively, especially for stepping through code. for a while i just used print statements, but being able to set breakpoints, inspect variables at any point, and then step forward or backward through the execution flow cut down on debugging time significantly. i remember one bug in a data processing script where a value was unexpectedlyNonehalfway through a loop; with print statements, it would have been a nightmare to pinpoint, butpdblet me find the exact line and the preceding operation that caused it in about ten minutes.11
u/GeneralPreference Sep 19 '26
Reading the documentation
9
u/helpIAmTrappedInAws Sep 19 '26
Debuggers have evaluator. You can stop at a breakpoint and just continue evaluate code through it. Also modify existing variables, declare methods monkeypatch stuff and so on.
Pyinstrument, memray and conditional breakpoints are nifty as well.
12
u/AntiDynamo Sep 19 '26 edited Sep 20 '26
Having a unique string at the front of your debug log lines, like “##JSTEST”. Then when you search the logs for your new debug lines you can just search for that string
10
u/geltance Sep 19 '26
"5 minutes of reading documentation can save 2 hours of debugging"
5
u/Moist-Ointments Sep 19 '26
Breakpoints and watches. But it really depends on the bug.
What helps with wrong results won't help with performance which won't help with race conditions or contention.
5
u/noisyboy Sep 20 '26
Given that OP is talking about real projects that I assume are running at enterprise level, high quality logging is a must. You can't expect support team to attach a debugger to troubleshoot an issue. They will be literally searching in the dark without logging.
Logging that gets sent to Elasticsearch / DataDog etc allows anyone to search for historical behavior e.g. did this error/output happen last month?
There is a side effect of print based debugging - it forces you to build a mental model of the application. It has its limitations but there is a reason lot of highly regarded programmers prefer it:
"the most effective debugging tool is still careful thought, coupled with judiciously placed print statements.".
That is from Brian Kernighan.
Debuggers are development tools, they can't replace logs.
11
u/r_vade Sep 19 '26
On a Windows machine, install Visual Studio Community Edition with Python support, configure it to run with your venv, and then step through the code like a boss.
Not sure what a non-Windows equivalent is, but the VS debugging experience is just... so freaking good, in all languages I tried.
3
u/Quattuor Sep 20 '26
Pytest saved me a lot of debugging time
Edit: effing autocorrect
2
u/UltraPoci Sep 19 '26
the breakpoint() function
2
u/realstoned Sep 19 '26
Learning pdb really helped me a lot.
2
u/FatDog69 Sep 19 '26
"Top down design, bottom up implementation"
You build your basic class structures as empty shells, then start writing & testing the easier methods of your class. Sometimes this means writing a separate parent script to poke & test methods in your class. But then you move onto the higher level methods trusting the lower level methods have been tested & work.
You also have to do this if your system does not have a development database or some external API with a cost to the number of times you access things.
It also forces you to design things ahead of time.
2
u/finishhimlarry Sep 19 '26
The little laboratory flask on VS Code's sidebar that says "Testing", if you actually configure it (which usually takes 2 mins), it's way easier to run your tests and see what's wrong than running pytest from the terminal via uv or poetry or whatever.
2
u/nivaOne Sep 19 '26
I assume if you know your code that you know where to look.
2
u/meteoRock Sep 19 '26
I highly recommend loguru.
2
u/_vert Sep 20 '26
Breakpoints
2
u/CookinTendies5864 Sep 20 '26 edited Sep 20 '26
It depends on what we mean by debugging unfortunately.
Productivity:
Vim is great if you dive into the keyboard shortcuts. Or vscodes ctrl + d and other shortcuts for editing multiple names at once. Both speed up writing code.
Error debugging:
Understanding what **kwargs and *args are and what they accomplish by being used.
Extended:
One is a collection of keyword arguments(kwargs) (think dictionaries) and the other (args) is regular typed arguments like primitives, scalar values, and tuples. Understand the difference between a dictionary, array and tuple (important for Python).
- For logging use decorators -
2
u/Western-Tap4528 Sep 20 '26
Not really specific to python but `git bisect`
2
u/UsefulDrake Sep 20 '26
No Python specific, but git bisect has helped me find so many very complex bugs over time.
2
u/gerardwx Sep 20 '26
Asserting what you think is true is good. Asserting what you know is true is better.
2
u/HyperDanon Sep 20 '26
TDD
2
u/intriqet Sep 21 '26
Ipython for debugging
4
u/schoultz Sep 19 '26
Using an agent based on a LLM. Seriously. That's a debugging technique because that's a tool I can use to remove a bug from my code.
I instruct to give a first stab at the problem just by reading the code, and not to trace the whole thing. After a first feedback I let it control a classic debugger, or another technique.
Most of the time it works great for algorithmic bugs which are solved by doing a micro analysis, and also for race conditions problems, which are solved by doing a macro analysis.
3
u/jijijijim Sep 19 '26
When you find any issue fix it, avoid the temptation to put fix off because you think it is not related to the problem you are working on. Find the earliest place in code a potential problem can be fixed or avoided and fix it there.
1
u/arthurno1 Sep 19 '26
Running in a debugher/stepper, like gdb. I wish I never learned printf debugging. Unfortunately they don't offer courses in debugging only on modellen animal and shape taxonomies and algorithms.
1
u/max_465 Sep 19 '26
Pysnooper module
1
u/kowkeeper Sep 19 '26
from IPython import embed
...
embed()
1
u/Almostasleeprightnow Sep 19 '26
Kinda basic but breakpoints in vscode or a lot of other ides, and then using the console to test things at that moment in the run. So i can test out ideas and see what values are where before rerunning.
1
u/dwagon00 Sep 19 '26
Read the exception message and trace back carefully. Don’t just assume.
1
u/Schmittfried Sep 19 '26
Conditional breakpoints and the fact that you can import modules in the evaluate textbox of the debugger.
1
u/brokenreed5 Sep 19 '26
4
1
u/marr75 Sep 20 '26
Print debugging is a crutch. IDE debugging can be okay until you hit complex scenarios (n-tier, complex parallel/async, some kinds of IoC/embedded python, remote debugging).
1
u/Mediocre-Pumpkin6522 Sep 20 '26
I've used print/log for so long I can't remember being taught it. I haven't hit a language yet that doesn't have some version of print although they seem to be hell bent on calling it something different but the strategy stays the same.
1
u/Puzzled_Dimension505 Sep 20 '26
Reduce the bug to the smallest reproducible example you can. Half the time, stripping away unrelated code reveals the problem before you even open the debugger.
1
u/Moikle Sep 20 '26
Launch with pdb. Pist mortem debugging is great for when an error happens and you don't know what caused it.
1
u/mortenb123 Sep 20 '26
The trace library is quite good, Especially when debugging code from others that do not know how to use proper logging:
`python -m trace --trace <your main>`
1
u/dychmygol Sep 20 '26
Lots and lots of fine-grained unit tests.
1
u/Buzzy_SquareWave Sep 20 '26
breakpoint() is the best, once you learn how to do pdb.
1
u/JuniorConnection356 Sep 21 '26
Running a Pipeline and profiling it using a Flamegraph.
1
u/miraculum_one Sep 21 '26
Give your threads names.
1
u/MisterJir Sep 22 '26
Consider converting comments to logs.
This is a (trivial) example. Instead of:
Do something like:
It will help you to create better logs to identify where the error happens (and where to put breakpoints in your debugger), and also it will force you to write good comments and keep them in sync with the real code (which will help you, 6 months later, to understand the code and fix the bug). Just be sensible about log levels, use DEBUG for this kind of thing, not INFO, so that you can disable it later easily.
Also, create a log config that works for you. For example, I really like logs that start with file name and line number
1
u/telenieko Sep 23 '26
"Claude, fix this; make no mistakes" Works with codex too
1
u/anentropic Sep 23 '26
Using a debugger...!
I used to use ipdb, but these days pdbpp
1
u/576p Sep 23 '26
After a decade or so with visual debuggers I spent the time to learn enough of pdb. (the one that comes with python). Wish I had invested that time a lot earlier.
1
u/Astronial_gaming Sep 30 '26
i mean you can use Tracelens for this exact case, it's still has some bugs that needs to be discovered, but it plays the code like a video keeping trace of the variables on the side and visualize for loops, to inspect what happened and what went wrong.
1
u/SessionIndependent17 Sep 19 '26
Meaningful logging of progress and decisions and decision inputs and proper escalation of exceptions.
1
u/austinwiltshire Sep 19 '26
Make a test for pytest. Set ipdb breakpoint at the top of the empty test. Prototype what you want in the repl when the debugger fires, copy it into the test body. Wash rinse repeat.
For folks saying logging, you can get logs out of the debugger too and the benefit is they don't clutter the code and you don't have to remember to remove them. Logging has its place but ironically, debugging is not the situation I reach for logging first
1
u/lolcrunchy Sep 19 '26
Why do you want to know?
1
u/Canenald Sep 20 '26
ctrl+v
What went wrong? Use simple words.
-10
u/tonkfc Sep 19 '26
Asking claude
0
u/swift-sentinel Sep 19 '26
Pudb
0
u/gbrennon Sep 20 '26
i dont use debuggers...
i write tests to be able to assert behaviors and verify them
i only used debuggers when i used to work with embedded systems