From c2ae2ea3e78a7f974f1c22c2828d1556081cb793 Mon Sep 17 00:00:00 2001 From: Vineet Naik Date: Sat, 31 Jul 2021 11:20:25 +0530 Subject: [PATCH] Fix --freeze option due to breaking changes in pip's internal api The 'pip._internal.metadata' modules were introduced in 21.1.1 and the 'pip._internal.operations.freeze.FrozenRequirement' class now expects dist to be a subclass of 'pip._internal.metadata.BaseDistribution', however the 'pip._internal.utils.misc.get_installed_distributions' continues to return objects of type pip._vendor.pkg_resources.DistInfoDistribution. This is a hacky backward compatible (with older versions of pip) fix. Fixes github issue #150. --- pipdeptree.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/pipdeptree.py b/pipdeptree.py index e00a88d..a0f68c1 100644 --- a/pipdeptree.py +++ b/pipdeptree.py @@ -74,6 +74,23 @@ def guess_version(pkg_key, default='?'): def frozen_req_from_dist(dist): + # The `pip._internal.metadata` modules were introduced in 21.1.1 + # and the `pip._internal.operations.freeze.FrozenRequirement` + # class now expects dist to be a subclass of + # `pip._internal.metadata.BaseDistribution`, however the + # `pip._internal.utils.misc.get_installed_distributions` continues + # to return objects of type + # pip._vendor.pkg_resources.DistInfoDistribution. + # + # This is a hacky backward compatible (with older versions of pip) + # fix. + try: + from pip._internal import metadata + except ImportError: + pass + else: + dist = metadata.pkg_resources.Distribution(dist) + try: return FrozenRequirement.from_dist(dist) except TypeError: