How to silence `pkg_resources` warnings

I’m trying to silence pkg_resources deprecation warnings but I don’t seem to be able to tame them:

# First attempt
❯ python -W ignore:pkg_resources -c "from kedro_datasets.pandas import CSVDataSet"
/Users/juan_cano/.micromamba/envs/kedro310-dev/lib/python3.10/site-packages/pkg_resources/__init__.py:121: DeprecationWarning: pkg_resources is deprecated as an API
  warnings.warn("pkg_resources is deprecated as an API", DeprecationWarning)
# Second attempt
❯ python -W ignore::DeprecationWarning -c "from kedro_datasets.pandas import CSVDataSet"
/Users/juan_cano/.micromamba/envs/kedro310-dev/lib/python3.10/site-packages/pkg_resources/__init__.py:121: DeprecationWarning: pkg_resources is deprecated as an API
  warnings.warn("pkg_resources is deprecated as an API", DeprecationWarning)
# Third attempt
>>> import warnings
>>> warnings.simplefilter("ignore", DeprecationWarning)
>>> from kedro_datasets.pandas import CSVDataSet
/Users/juan_cano/.micromamba/envs/kedro310-dev/lib/python3.10/site-packages/pkg_resources/__init__.py:121: DeprecationWarning: pkg_resources is deprecated as an API
  warnings.warn("pkg_resources is deprecated as an API", DeprecationWarning)
# Fourth and desperate attempt
>>> import warnings
>>> warnings.simplefilter("ignore")
>>> from kedro_datasets.pandas import CSVDataSet
/Users/juan_cano/.micromamba/envs/kedro310-dev/lib/python3.10/site-packages/pkg_resources/__init__.py:121: DeprecationWarning: pkg_resources is deprecated as an API
  warnings.warn("pkg_resources is deprecated as an API", DeprecationWarning)

Any pointers? I’m not sure why these warnings seem to behave differently.

1 Like

Per the docs for warnings, this is telling Python that you want to treat DeprecationWarning as an error. I think what you actually want is to ignore it:

warnings.simplefilter("ignore", DeprecationWarning)

Thanks @jamestwebber , but either action doesn’t work. I’ll update the body of the post for consistency, but here’s what happens if I try to silence:

❯ python -W ignore:pkg_resources -c "from kedro_datasets.pandas import CSVDataSet"                                                                                                                                                                                                 (kedro310-dev) 
/Users/juan_cano/.micromamba/envs/kedro310-dev/lib/python3.10/site-packages/pkg_resources/__init__.py:121: DeprecationWarning: pkg_resources is deprecated as an API
  warnings.warn("pkg_resources is deprecated as an API", DeprecationWarning)
/Users/juan_cano/.micromamba/envs/kedro310-dev/lib/python3.10/site-packages/pkg_resources/__init__.py:2870: DeprecationWarning: Deprecated call to `pkg_resources.declare_namespace('google')`.
Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages
  declare_namespace(pkg)
/Users/juan_cano/.micromamba/envs/kedro310-dev/lib/python3.10/site-packages/pkg_resources/__init__.py:2870: DeprecationWarning: Deprecated call to `pkg_resources.declare_namespace('google.cloud')`.
Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages
  declare_namespace(pkg)
/Users/juan_cano/.micromamba/envs/kedro310-dev/lib/python3.10/site-packages/pkg_resources/__init__.py:2349: DeprecationWarning: Deprecated call to `pkg_resources.declare_namespace('google')`.
Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages
  declare_namespace(parent)
/Users/juan_cano/.micromamba/envs/kedro310-dev/lib/python3.10/site-packages/pkg_resources/__init__.py:2870: DeprecationWarning: Deprecated call to `pkg_resources.declare_namespace('google.logging')`.
Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages
  declare_namespace(pkg)
/Users/juan_cano/.micromamba/envs/kedro310-dev/lib/python3.10/site-packages/pkg_resources/__init__.py:2870: DeprecationWarning: Deprecated call to `pkg_resources.declare_namespace('mpl_toolkits')`.
Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages
  declare_namespace(pkg)
/Users/juan_cano/.micromamba/envs/kedro310-dev/lib/python3.10/site-packages/google/rpc/__init__.py:20: DeprecationWarning: Deprecated call to `pkg_resources.declare_namespace('google.rpc')`.
Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages
  pkg_resources.declare_namespace(__name__)

Testing:

$ python -c "import pkg_resources; pkg_resources.declare_namespace('test')"
<string>:1: DeprecationWarning: Deprecated call to `pkg_resources.declare_namespace('test')`.
Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages

I can suppress all deprecation warnings:

$ python -W 'ignore::DeprecationWarning' -c "import pkg_resources; pkg_resources.declare_namespace('test')"

Suppressing this specific warning by suppressing them for the pkg_resources module doesn’t sem to work:

$ python -W 'ignore::DeprecationWarning:pkg_resources' -c "import pkg_resources; pkg_resources.declare_namespace('test')"       
<string>:1: DeprecationWarning: Deprecated call to `pkg_resources.declare_namespace('test')`.
Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages

Guessing that’s because of the stacklevel=2. But we can match the start of the message:

$ python -W 'ignore:Deprecated call to `pkg_r:DeprecationWarning' -c "import pkg_resources; pkg_resources.declare_namespace('test')"

It won’t take a regex, but it doesn’t require matching the entire string.

Thanks @effigies , but we’re still not quite there.

It seems to be possible to silence warnings that come after calling pkg_resources directly. But if your code triggers a pkg_resources call somewhere else, not even -Wignore (ignore all warnings) will work.

Well, the problem was that there was a global warnings.simplefilter("default", DeprecationWarning) hidden in one of the modules I was importing, which was overriding everything I was doing. Case closed, for now. Sorry all for the noise!

1 Like

Yeah, the global registry is painful:

$ python -Wignore -c "import warnings; warnings.resetwarnings(); warnings.warn('Damn')"
<string>:1: UserWarning: Damn
1 Like

Woah, exactly this. Thanks again!