From 457cf31da61a2ca3fbe7c91e11b23adb25d2c2b9 Mon Sep 17 00:00:00 2001 From: Marc Abramowitz Date: Thu, 19 Mar 2015 09:48:18 -0700 Subject: DistributionNotFound: Show requirers It is very helpful to know who required the missing package. E.g.: pkg_resources.DistributionNotFound: The 'colorama>=0.3.1' distribution was not found and is required by smlib.log. Note that there was a comment: "unfortunately, zc.buildout uses a str(err) to get the name of the distribution here..", but I did a search in the `buildout` code and I think that is no longer true, so I think that we're free to make the exception message more helpful without risk of breaking buildout: # In clone from https://github.com/buildout/buildout $ ag DistributionNotFound src/zc/buildout/buildout.py 686: except (ImportError, pkg_resources.DistributionNotFound): $ pip install --download=. --no-install zc.buildout ... Saved ./zc.buildout-2.3.1.tar.gz ... $ tar xf zc.buildout-2.3.1.tar.gz $ ag DistributionNotFound zc.buildout-2.3.1 zc.buildout-2.3.1/src/zc/buildout/buildout.py 686: except (ImportError, pkg_resources.DistributionNotFound): --- pkg_resources/__init__.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/pkg_resources/__init__.py b/pkg_resources/__init__.py index c3686f88..4e820c09 100644 --- a/pkg_resources/__init__.py +++ b/pkg_resources/__init__.py @@ -798,13 +798,16 @@ class WorkingSet(object): ws = WorkingSet([]) dist = best[req.key] = env.best_match(req, ws, installer) if dist is None: - #msg = ("The '%s' distribution was not found on this " - # "system, and is required by this application.") - #raise DistributionNotFound(msg % req) - - # unfortunately, zc.buildout uses a str(err) - # to get the name of the distribution here.. - raise DistributionNotFound(req) + requirers = required_by.get(req, None) + if requirers: + requirers_str = ', '.join(requirers) + else: + requirers_str = 'this application' + msg = ("The '%s' distribution was not found " + "and is required by %s." + % (req, requirers_str)) + warnings.warn(msg) + raise DistributionNotFound(msg) to_activate.append(dist) if dist not in req: # Oops, the "best" so far conflicts with a dependency -- cgit v1.2.1 From b24eb2cc8b7d95d39997e3b79c94544fcf7cefeb Mon Sep 17 00:00:00 2001 From: Marc Abramowitz Date: Tue, 24 Mar 2015 08:20:42 -0700 Subject: DistributionNotFound: Move message template to class --- pkg_resources/__init__.py | 34 +++++++++++++++++++++++++--------- setuptools/command/easy_install.py | 4 +--- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/pkg_resources/__init__.py b/pkg_resources/__init__.py index 4e820c09..6f1ab9b7 100644 --- a/pkg_resources/__init__.py +++ b/pkg_resources/__init__.py @@ -368,6 +368,30 @@ class ContextualVersionConflict(VersionConflict): class DistributionNotFound(ResolutionError): """A requested distribution was not found""" + _template = ("The '{self.req}' distribution was not found " + "and is required by {self.requirers_str}") + + @property + def req(self): + return self.args[0] + + @property + def requirers(self): + return self.args[1] + + @property + def requirers_str(self): + if not self.requirers: + return 'the application' + return ', '.join(self.requirers) + + def report(self): + return self._template.format(**locals()) + + def __str__(self): + return self.report() + + class UnknownExtra(ResolutionError): """Distribution doesn't have an "extra feature" of the given name""" _provider_factories = {} @@ -799,15 +823,7 @@ class WorkingSet(object): dist = best[req.key] = env.best_match(req, ws, installer) if dist is None: requirers = required_by.get(req, None) - if requirers: - requirers_str = ', '.join(requirers) - else: - requirers_str = 'this application' - msg = ("The '%s' distribution was not found " - "and is required by %s." - % (req, requirers_str)) - warnings.warn(msg) - raise DistributionNotFound(msg) + raise DistributionNotFound(req, requirers) to_activate.append(dist) if dist not in req: # Oops, the "best" so far conflicts with a dependency diff --git a/setuptools/command/easy_install.py b/setuptools/command/easy_install.py index 276b6f99..f2bfa68d 100755 --- a/setuptools/command/easy_install.py +++ b/setuptools/command/easy_install.py @@ -709,9 +709,7 @@ class easy_install(Command): [requirement], self.local_index, self.easy_install ) except DistributionNotFound as e: - raise DistutilsError( - "Could not find required distribution %s" % e.args - ) + raise DistutilsError(str(e)) except VersionConflict as e: raise DistutilsError(e.report()) if self.always_copy or self.always_copy_from: -- cgit v1.2.1