Building on this anwser, you can to this by:
- Changing the default function used to print warnings to a custom one
- Set a normal breakpoint anywhere inside it (with your favourite debugger / IDE )
- Use the debugger to go up one level in the stack, to exit the warning printing function, and go to where the warning was actually generated
This is an example, custom warning printing function:
import traceback
import warnings
import sys
def warn_with_traceback(message, category, filename, lineno, file=None, line=None):
# Set a BREAKPOINT on any of these lines below !
log = file if hasattr(file,'write') else sys.stderr
traceback.print_stack(file=log)
log.write(warnings.formatwarning(message, category, filename, lineno, line))
warnings.showwarning = warn_with_traceback
Also this particular function displays the traceback of the error. Which is always helpful in debugging.