It turns out that you can allow warnings.warn()to collect all the information and just program the way to print information:
import warnings
def warning_on_one_line(message, category, filename, lineno, file=None, line=None):
return '%s:%s: %s: %s\n' % (filename, lineno, category.__name__, message)
warnings.formatwarning = warning_on_one_line
warnings.warn('Run Forest run!', stacklevel=2)
warnings.warn('Run Forest run!')
Conclusion:
sys:1: UserWarning: Run Forest run!
./file.py:15: UserWarning: Run Forest run!
Source: Python module of the week
arney source
share