22

I was setting up a ImportWarning as seemed appropriate but noticed this warning is not reported by default;

How can I set python to report ImportWarning or all warnings?

Here is the import warning i wrote:

try:
    from markdown import markdown

except ImportError, err:
    warnings.warn(
        'Unable to load Pypi package `markdown`, HTML output will be unavailable. {}'.format(err),
        ImportWarning
    )
1
  • @rpattiso Ahh, I was using a shebang line to run my script, running with the python interpreter and the W flag did make the warning appear! Commented Apr 21, 2015 at 23:25

3 Answers 3

25

To enable warnings run python with the -Wdefault or -Wd switch.

Sign up to request clarification or add additional context in comments.

Comments

15
import warnings
warnings.simplefilter('module')

Or:

import warnings
warnings.simplefilter('always')

The list of filters are in the docs

Comments

2

You can also enable warnings for just one section of code:

import warnings
with warnings.catch_warnings():
    warnings.simplefilter('always')
    # all warnings here are enabled
# warnings here are left as default (probably silent)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.