how to convert all warnings to errors?

Alex Martelli aleax at
Tue Oct 1 21:46:28 CEST 2002

Skip Montanaro wrote:

> I thought there was a simple command line flag to turn warnings into
> errors,
> but I can't seem to find it.  Pointers?

python -W error

should do it.  E.g.:

[alex at arthur alex]$ cat
import sys, warnings
print 'before'
warnings.warn(' '.join(sys.argv[1:]))
print 'after'
[alex at arthur alex]$ python one two
before UserWarning: one two
  warnings.warn(' '.join(sys.argv[1:]))
[alex at arthur alex]$ python -W error one two
Traceback (most recent call last):
  File "", line 3, in ?
    warnings.warn(' '.join(sys.argv[1:]))
  File "/usr/local/lib/python2.2/", line 42, in warn
    warn_explicit(message, category, filename, lineno, module, registry)
  File "/usr/local/lib/python2.2/", line 71, in warn_explicit
    raise category(message)
UserWarning: one two
[alex at arthur alex]$

More generally, -W lets you specify conditions that warnings must match
as well as actions (such as error) to perform when they do.  python -h
mentions this (rather summarily, of course) and the online docs for module
warnings gives more details.


More information about the Python-list mailing list