From 455b77f9a89f3bfded5925ccc15e17d20c9b99cd Mon Sep 17 00:00:00 2001 From: Anderson Bravalheri Date: Wed, 1 May 2024 16:27:31 +0100 Subject: [PATCH] Account for importlib_metadata in Python<3.7 --- dev-requirements.txt | 1 + src/pyproject_hooks/_in_process/_in_process.py | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/dev-requirements.txt b/dev-requirements.txt index 4a8c47a..69b161f 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -3,3 +3,4 @@ flake8 testpath setuptools>=30 tomli >=1.1.0 ; python_version<'3.11' +importlib-metadata; python_version<'3.8' diff --git a/src/pyproject_hooks/_in_process/_in_process.py b/src/pyproject_hooks/_in_process/_in_process.py index 0b49c35..4f0bc24 100644 --- a/src/pyproject_hooks/_in_process/_in_process.py +++ b/src/pyproject_hooks/_in_process/_in_process.py @@ -108,7 +108,12 @@ def find_spec(self, fullname, _path, _target=None): def find_distributions(self, context=None): # Delayed import: Python 3.7 does not contain importlib.metadata - from importlib.metadata import DistributionFinder, MetadataPathFinder + # If this method is being called it must be because + # `importlib.metadata`/`importlib_metadata` is available. + try: + from importlib_metadata import DistributionFinder, MetadataPathFinder + except ImportError: + from importlib.metadata import DistributionFinder, MetadataPathFinder context = DistributionFinder.Context(path=self.backend_path) return MetadataPathFinder.find_distributions(context=context)