Check if an environment's packages conflict

Hello folks,

I’m trying to see if there is a way to see if the current environment’s dependencies appear compatible with each other. I also recognize this makes more sense at install time than runtime, though I don’t have a way to modify the install-time process for my work projects. That brought me to pkg_resources.require.

Is there a modern equivalent to pkg_resources.require in importlib (or somewhere else)?

There is importlib.metadata.requires but that just gives the requirements, but doesn’t tell if the requirements are met/violated in the current environment.

If I ignore the pkg_resources deprecation, this pytest seems to catch environment compatibility issues:

from importlib.metadata import packages_distributions
from pkg_resources import require


def test_package_sanity():
    """
    This test is a simple sanity check. It will fail if the dependencies are incompatible.
    """
    issues = set()
    for packages in packages_distributions().values():
        for package in packages:
            try:
                require(package)
            except Exception as e:
                issues.add(repr(e))

    if issues:
        raise ValueError("Issues with dependency compatibility:\n" + '\n'.join(issues))

Is there a cli or third party package that basically does ^ already?

Is there a cli or third party package that basically does ^ already?

Usage:
  pip check [options]

Description:
  Verify installed packages have compatible dependencies.
1 Like

Cross-link:

importlib.metadata.requires
packaging.requirements.Requirement
In general can build a package management system without pip.