Skip to content

Instantly share code, notes, and snippets.

@toby-p
Created March 25, 2020 19:37
Show Gist options
  • Save toby-p/486a7377af6485925cb3edd2cc007190 to your computer and use it in GitHub Desktop.
Save toby-p/486a7377af6485925cb3edd2cc007190 to your computer and use it in GitHub Desktop.
Show Python warnings with full traceback (from: https://stackoverflow.com/questions/22373927/get-traceback-of-warnings)
import traceback
import warnings
import sys
def warn_with_traceback(message, category, filename, lineno, file=None, line=None):
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment