Confusing behavior of PYTHONWARNINGS

Roel Schroeven roel at roelschroeven.net
Mon Sep 18 04:14:20 EDT 2023


Op 16/09/2023 om 10:17 schreef Meowxiik via Python-list:
> Hello,
>
> For the third time I am trying to work with `PYTHONWARNINGS` filter, 
> and for the third time I am having a terrible time. I'd like to seek 
> your assistance, I have a few questions:
>
> **Question 1:** Does the environment variable only matter at python 
> interpreter startup time? If I set the same variable before python 
> start, it seems to work, but it doesn't work trough os.environ. It 
> does appear to be hinted at in the docs, but imho not clear enough.
Yes, environment variables are only relevant when the interpreter starts 
up. The documentation (on the command line environment in general, not 
specific to PYTHONWARNINGS) says "These environment variables influence 
Python’s behavior, they are processed before the command-line switches 
other than -E or -I.". That implies they don't do anything if changed 
after startup.

More specific to warnings, the documentation for the -W option (which is 
referenced in the PYTHONWARNINGS environment variable) says "Warnings 
can also be controlled using the PYTHONWARNINGS environment variable and 
from within a Python program using the warnings module." implying the 
same, and giving an alternative for controlling warnings from within Python.

> **Question 2:** Why do the following filters not work for filtering 
> `urllib3.exceptions.InsecureRequestWarning`
>
> - `PYTHONWARNINGS=ignore:::urllib3.exceptions`
>
> - `PYTHONWARNINGS=ignore:::urllib3`
>
> - `PYTHONWARNINGS=ignore:::urllib3.exceptions`
>
> - `PYTHONWARNINGS=ignore::urllib3.exceptions.InsecureRequestWarning`
>
> None of these filter the warning. The last one has the audacity to 
> complain about "invalid module name: 'urllib3.exceptions'" which is 
> very confusing to me, given that that specific module is fully 
> importable during runtime, which I have tested. The only one I managed 
> to get working is `ignore:Unverified`, where "Unverified" is the first 
> word of the actual message, however I truly strongly dislike this 
> solution.
I'd like to try this out myself, but I'm not familiar with urllib3 and I 
can't get to issue that warning; instead it throws exceptions (I tried a 
simple request with some of the URL's listed on https://badssl.com). How 
can I get urllib3 to issue that warning?

Also, which version of Python are you using, and on which operating system?

-- 
"Most of us, when all is said and done, like what we like and make up
reasons for it afterwards."
         -- Soren F. Petersen



More information about the Python-list mailing list