Sos
Sos

Reputation: 1949

Python warnings.warn() not returning anything

I'm using warnings.warn(), together with a print, but the warnings are not returned:

if[1!=2]:
    print('Error')
    warnings.warn('Warning!')
#returns:  Error

I'm using Jupyter. Would appreciate if someone could assist

Upvotes: 5

Views: 2438

Answers (2)

PatrickT
PatrickT

Reputation: 10510

This is a duplicate of this question. Several answers there explain that this is by design. To override the default, set the keyword to "always", as follows:

import warnings
warnings.simplefilter('always', UserWarning)

Hat tip to Selcuk and Martijn Pieters, among others

Upvotes: 0

Danny Denenberg
Danny Denenberg

Reputation: 49

I was having the same issue. My solution was to switch to python's logging package.

import logging
logging.warning("Warning!")

It printed to the console on my end!

Upvotes: 4

Related Questions