Search code examples
pythonpipsetuptoolsdeprecation-warning

pkg_resources is deprecated as an API


When I try to install from a .tar.gz package, while making warnings into errors:

python -W error -m pip install /some/path/nspace.pkga-0.1.0.tar.gz

I get this error:

ERROR: Exception:
Traceback (most recent call last):
  File "/opt/util/nspace1/lib/python3.11/site-packages/pip/_internal/cli/base_command.py", line 169, in exc_logging_wrapper
    status = run_func(*args)
             ^^^^^^^^^^^^^^^
  File "/opt/util/nspace1/lib/python3.11/site-packages/pip/_internal/cli/req_command.py", line 248, in wrapper
    return func(self, options, args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/util/nspace1/lib/python3.11/site-packages/pip/_internal/commands/install.py", line 324, in run
    session = self.get_default_session(options)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/util/nspace1/lib/python3.11/site-packages/pip/_internal/cli/req_command.py", line 98, in get_default_session
    self._session = self.enter_context(self._build_session(options))
                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/util/nspace1/lib/python3.11/site-packages/pip/_internal/cli/req_command.py", line 125, in _build_session
    session = PipSession(
              ^^^^^^^^^^^
  File "/opt/util/nspace1/lib/python3.11/site-packages/pip/_internal/network/session.py", line 342, in __init__
    self.headers["User-Agent"] = user_agent()
                                 ^^^^^^^^^^^^
  File "/opt/util/nspace1/lib/python3.11/site-packages/pip/_internal/network/session.py", line 175, in user_agent
    setuptools_dist = get_default_environment().get_distribution("setuptools")
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/util/nspace1/lib/python3.11/site-packages/pip/_internal/metadata/importlib/_envs.py", line 188, in get_distribution
    return next(matches, None)
           ^^^^^^^^^^^^^^^^^^^
  File "/opt/util/nspace1/lib/python3.11/site-packages/pip/_internal/metadata/importlib/_envs.py", line 183, in <genexpr>
    matches = (
              ^
  File "/opt/util/nspace1/lib/python3.11/site-packages/pip/_internal/metadata/base.py", line 612, in iter_all_distributions
    for dist in self._iter_distributions():
  File "/opt/util/nspace1/lib/python3.11/site-packages/pip/_internal/metadata/importlib/_envs.py", line 176, in _iter_distributions
    for dist in finder.find_eggs(location):
  File "/opt/util/nspace1/lib/python3.11/site-packages/pip/_internal/metadata/importlib/_envs.py", line 144, in find_eggs
    yield from self._find_eggs_in_dir(location)
  File "/opt/util/nspace1/lib/python3.11/site-packages/pip/_internal/metadata/importlib/_envs.py", line 111, in _find_eggs_in_dir
    from pip._vendor.pkg_resources import find_distributions
  File "/opt/util/nspace1/lib/python3.11/site-packages/pip/_vendor/pkg_resources/__init__.py", line 121, in <module>
    warnings.warn("pkg_resources is deprecated as an API", DeprecationWarning)
DeprecationWarning: pkg_resources is deprecated as an API

pip seems to have vendored in a deprecated package. The pip code responsible is in pip/_internal/metadata/importlib/_envs.py in class Environment:

    def _iter_distributions(self) -> Iterator[BaseDistribution]:
        finder = _DistributionFinder()
        for location in self._paths:
            yield from finder.find(location)
            for dist in finder.find_eggs(location):
                # _emit_egg_deprecation(dist.location)  # TODO: Enable this.
                yield dist
            # This must go last because that's how pkg_resources tie-breaks.
            yield from finder.find_linked(location)

If I comment out the nested for loop (doing the find_eggs) thinks work fine: I get no error and a working package installed.

How do I monkeypatch that Environment instance from my setup.py file?

This is Python 3.11.3 (so it should be using importlib.metadata and not pkg_resources) on macOS, pip==23.1, setuptools==67.6.1

Background: I am just trying this out on an example package, the reason for this is based on a bug report for my ruamel.yaml package, where it is build in such a less forgiving environment. I could of course say don't use -W error, but I rather solve this, by not invoking the offending, unused code in the first place


Solution

  • There is a related discussion on pip's ticket tracker.

    It seems like this issue has been solved in pip 23.1.1: "Revert pkg_resources (via setuptools) back to 65.6.3".

    And pip 23.1.2 seems to vendor the new setuptools (and pkg_resources) as expected but without the deprecation warnings (see also this message).