Repository: mne-tools/mne-python

Issue:
many CI failures related to pkg_resources
At first I thought it was just nilearn so I opened this: https://github.com/nilearn/nilearn/pull/3512 but it's also happening with pybtex.

The meat of it is: 

> DeprecationWarning: 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

it happens on the line `import pkg_resources`. Here's a full traceback for the pybtex case:

<details>

```
Traceback (most recent call last):
  File "/home/circleci/python_env/lib/python3.10/site-packages/sphinx/cmd/build.py", line 276, in build_main
    app = Sphinx(args.sourcedir, args.confdir, args.outputdir,
  File "/home/circleci/python_env/lib/python3.10/site-packages/sphinx/application.py", line 223, in __init__
    self.setup_extension(extension)
  File "/home/circleci/python_env/lib/python3.10/site-packages/sphinx/application.py", line 398, in setup_extension
    self.registry.load_extension(self, extname)
  File "/home/circleci/python_env/lib/python3.10/site-packages/sphinx/registry.py", line 459, in load_extension
    mod = import_module(extname)
  File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/home/circleci/python_env/lib/python3.10/site-packages/sphinxcontrib/bibtex/__init__.py", line 9, in <module>
    from .domain import BibtexDomain
  File "/home/circleci/python_env/lib/python3.10/site-packages/sphinxcontrib/bibtex/domain.py", line 20, in <module>
    import pybtex_docutils
  File "/home/circleci/python_env/lib/python3.10/site-packages/pybtex_docutils/__init__.py", line 34, in <module>
    from pybtex.backends import BaseBackend
  File "/home/circleci/python_env/lib/python3.10/site-packages/pybtex/backends/__init__.py", line 26, in <module>
    from pybtex.plugin import Plugin
  File "/home/circleci/python_env/lib/python3.10/site-packages/pybtex/plugin/__init__.py", line 26, in <module>
    import pkg_resources
  File "/home/circleci/python_env/lib/python3.10/site-packages/pkg_resources/__init__.py", line 3257, in <module>
    def _initialize_master_working_set():
  File "/home/circleci/python_env/lib/python3.10/site-packages/pkg_resources/__init__.py", line 3231, in _call_aside
    f(*args, **kwargs)
  File "/home/circleci/python_env/lib/python3.10/site-packages/pkg_resources/__init__.py", line 3282, in _initialize_master_working_set
    tuple(dist.activate(replace=False) for dist in working_set)
  File "/home/circleci/python_env/lib/python3.10/site-packages/pkg_resources/__init__.py", line 3282, in <genexpr>
    tuple(dist.activate(replace=False) for dist in working_set)
  File "/home/circleci/python_env/lib/python3.10/site-packages/pkg_resources/__init__.py", line 2803, in activate
    declare_namespace(pkg)
  File "/home/circleci/python_env/lib/python3.10/site-packages/pkg_resources/__init__.py", line 2297, in declare_namespace
    warnings.warn(msg, DeprecationWarning, stacklevel=2)
DeprecationWarning: 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
```

<details>


Discussion:
How difficult would it be to solve by removing `pkg_resources`? It should be easy for `bcidata`.
It needs to be done in our dependencies. For the nilearn PR above it's a one-line fix. I've just opened https://bitbucket.org/pybtex-devs/pybtex/issues/169/replace-pkg_resources-with for pybtex. IDK how many other ones we'll hit though once those two are cleared up.
Ah OK! I thought we could also remove it in MNE, because maybe we can work around it (which is definitely possible in `bcidata`, but I don't know about the other occurrence).
> Ah OK! I thought we could also remove it in MNE

We could catch / ignore the warnings but AFAICT this isn't something we can properly fix within our own codebase.

Resolving patch:
diff --git a/doc/conf.py b/doc/conf.py
--- a/doc/conf.py
+++ b/doc/conf.py
@@ -1007,6 +1007,9 @@ def reset_warnings(gallery_conf, fname):
         'Enum value .* is marked as deprecated',
         # matplotlib PDF output
         'The py23 module has been deprecated',
+        # pkg_resources
+        'Implementing implicit namespace packages',
+        'Deprecated call to `pkg_resources',
     ):
         warnings.filterwarnings(  # deal with other modules having bad imports
             'ignore', message=".*%s.*" % key, category=DeprecationWarning)
diff --git a/mne/conftest.py b/mne/conftest.py
--- a/mne/conftest.py
+++ b/mne/conftest.py
@@ -139,6 +139,9 @@ def pytest_configure(config):
     # PySide6
     ignore:Enum value .* is marked as deprecated:DeprecationWarning
     ignore:Function.*is marked as deprecated, please check the documentation.*:DeprecationWarning
+    # pkg_resources usage bug
+    ignore:Implementing implicit namespace packages.*:DeprecationWarning
+    ignore:Deprecated call to `pkg_resources.*:DeprecationWarning
     """.format(first_kind)  # noqa: E501
     for warning_line in warning_lines.split('\n'):
         warning_line = warning_line.strip()


Test patch:
diff --git a/mne/viz/_brain/tests/test_brain.py b/mne/viz/_brain/tests/test_brain.py
--- a/mne/viz/_brain/tests/test_brain.py
+++ b/mne/viz/_brain/tests/test_brain.py
@@ -1126,7 +1126,10 @@ def _create_testing_brain(hemi, surf='inflated', src='surface',
     return brain_data
 
 
-def test_foci_mapping(tmp_path, renderer_interactive_pyvistaqt):
+# TODO: allow_unclosed for macOS here as the conda build shows some
+# windows stay open afterward
+def test_foci_mapping(tmp_path, renderer_interactive_pyvistaqt,
+                      allow_unclosed):
     """Test mapping foci to the surface."""
     tiny_brain, _ = tiny(tmp_path)
     foci_coords = tiny_brain.geo['lh'].coords[:2] + 0.01