Repository: pypa/setuptools

Issue:
[BUG] vendored importlib.metadata of setuptools 60.9.0 fails with 'PathDistribution' object has no attribute '_normalized_name'
### setuptools version

setuptools==60.9.0

### Python version

3.7.1

### OS

Linux

### Additional environment information

Pip freeze

```
argcomplete==1.12.3
colorlog==6.6.0
distlib==0.3.4
filelock==3.4.2
importlib-metadata==4.2.0
nox==2022.1.7
packaging==21.3
pip==22.0.3
platformdirs==2.5.0
py==1.11.0
pyparsing==3.0.7
setuptools==60.9.0
six==1.16.0
typing_extensions==4.1.1
virtualenv==20.13.1
wheel==0.37.1
zipp==3.7.0
```

### Description

If I use the below python dependencies (mainly virtualenv, setuptools and importlib.metadata), setuptools fails with an error.

It works fine with the previous version of setuptools and newer versions of importlib.metadata. It seems like, that the current vendored version of importlib.metadata is not compatible with my environment. 

### Expected behavior

It should work fine

### How to Reproduce

1. Install the above dependencies
2. Run `nox -s`

### Output

```console
Traceback (most recent call last):
  File "/usr/lib64/python3.7/runpy.py", line 183, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/usr/lib64/python3.7/runpy.py", line 142, in _get_module_details
    return _get_module_details(pkg_main_name, error)
  File "/usr/lib64/python3.7/runpy.py", line 109, in _get_module_details
    __import__(pkg_name)
  File "/root/git/empty_test/venv/lib/python3.7/site-packages/virtualenv/__init__.py", line 3, in <module>
    from .run import cli_run, session_via_cli
  File "/root/git/empty_test/venv/lib/python3.7/site-packages/virtualenv/run/__init__.py", line 14, in <module>
    from .plugin.creators import CreatorSelector
  File "/root/git/empty_test/venv/lib/python3.7/site-packages/virtualenv/run/plugin/creators.py", line 6, in <module>
    from virtualenv.create.via_global_ref.builtin.builtin_way import VirtualenvBuiltin
  File "/root/git/empty_test/venv/lib/python3.7/site-packages/virtualenv/create/via_global_ref/builtin/builtin_way.py", line 7, in <module>
    from virtualenv.create.creator import Creator
  File "/root/git/empty_test/venv/lib/python3.7/site-packages/virtualenv/create/creator.py", line 15, in <module>
    from virtualenv.discovery.cached_py_info import LogCmd
  File "/root/git/empty_test/venv/lib/python3.7/site-packages/virtualenv/discovery/cached_py_info.py", line 23, in <module>
    _CACHE[Path(sys.executable)] = PythonInfo()
  File "/root/git/empty_test/venv/lib/python3.7/site-packages/virtualenv/discovery/py_info.py", line 86, in __init__
    self.distutils_install = {u(k): u(v) for k, v in self._distutils_install().items()}
  File "/root/git/empty_test/venv/lib/python3.7/site-packages/virtualenv/discovery/py_info.py", line 152, in _distutils_install
    d = dist.Distribution({"script_args": "--no-user-cfg"})  # conf files not parsed so they do not hijack paths
  File "/root/git/empty_test/venv/lib/python3.7/site-packages/setuptools/dist.py", line 456, in __init__
    for ep in metadata.entry_points(group='distutils.setup_keywords'):
  File "/root/git/empty_test/venv/lib/python3.7/site-packages/setuptools/_vendor/importlib_metadata/__init__.py", line 1003, in entry_points
    return SelectableGroups.load(eps).select(**params)
  File "/root/git/empty_test/venv/lib/python3.7/site-packages/setuptools/_vendor/importlib_metadata/__init__.py", line 453, in load
    ordered = sorted(eps, key=by_group)
  File "/root/git/empty_test/venv/lib/python3.7/site-packages/setuptools/_vendor/importlib_metadata/__init__.py", line 1001, in <genexpr>
    dist.entry_points for dist in unique(distributions())
  File "/root/git/empty_test/venv/lib/python3.7/site-packages/setuptools/_vendor/importlib_metadata/_itertools.py", line 16, in unique_everseen
    k = key(element)
AttributeError: 'PathDistribution' object has no attribute '_normalized_name'
```
[BUG] vendored importlib.metadata of setuptools 60.9.0 fails with 'PathDistribution' object has no attribute '_normalized_name'
### setuptools version

setuptools==60.9.0

### Python version

3.7.1

### OS

Linux

### Additional environment information

Pip freeze

```
argcomplete==1.12.3
colorlog==6.6.0
distlib==0.3.4
filelock==3.4.2
importlib-metadata==4.2.0
nox==2022.1.7
packaging==21.3
pip==22.0.3
platformdirs==2.5.0
py==1.11.0
pyparsing==3.0.7
setuptools==60.9.0
six==1.16.0
typing_extensions==4.1.1
virtualenv==20.13.1
wheel==0.37.1
zipp==3.7.0
```

### Description

If I use the below python dependencies (mainly virtualenv, setuptools and importlib.metadata), setuptools fails with an error.

It works fine with the previous version of setuptools and newer versions of importlib.metadata. It seems like, that the current vendored version of importlib.metadata is not compatible with my environment. 

### Expected behavior

It should work fine

### How to Reproduce

1. Install the above dependencies
2. Run `nox -s`

### Output

```console
Traceback (most recent call last):
  File "/usr/lib64/python3.7/runpy.py", line 183, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/usr/lib64/python3.7/runpy.py", line 142, in _get_module_details
    return _get_module_details(pkg_main_name, error)
  File "/usr/lib64/python3.7/runpy.py", line 109, in _get_module_details
    __import__(pkg_name)
  File "/root/git/empty_test/venv/lib/python3.7/site-packages/virtualenv/__init__.py", line 3, in <module>
    from .run import cli_run, session_via_cli
  File "/root/git/empty_test/venv/lib/python3.7/site-packages/virtualenv/run/__init__.py", line 14, in <module>
    from .plugin.creators import CreatorSelector
  File "/root/git/empty_test/venv/lib/python3.7/site-packages/virtualenv/run/plugin/creators.py", line 6, in <module>
    from virtualenv.create.via_global_ref.builtin.builtin_way import VirtualenvBuiltin
  File "/root/git/empty_test/venv/lib/python3.7/site-packages/virtualenv/create/via_global_ref/builtin/builtin_way.py", line 7, in <module>
    from virtualenv.create.creator import Creator
  File "/root/git/empty_test/venv/lib/python3.7/site-packages/virtualenv/create/creator.py", line 15, in <module>
    from virtualenv.discovery.cached_py_info import LogCmd
  File "/root/git/empty_test/venv/lib/python3.7/site-packages/virtualenv/discovery/cached_py_info.py", line 23, in <module>
    _CACHE[Path(sys.executable)] = PythonInfo()
  File "/root/git/empty_test/venv/lib/python3.7/site-packages/virtualenv/discovery/py_info.py", line 86, in __init__
    self.distutils_install = {u(k): u(v) for k, v in self._distutils_install().items()}
  File "/root/git/empty_test/venv/lib/python3.7/site-packages/virtualenv/discovery/py_info.py", line 152, in _distutils_install
    d = dist.Distribution({"script_args": "--no-user-cfg"})  # conf files not parsed so they do not hijack paths
  File "/root/git/empty_test/venv/lib/python3.7/site-packages/setuptools/dist.py", line 456, in __init__
    for ep in metadata.entry_points(group='distutils.setup_keywords'):
  File "/root/git/empty_test/venv/lib/python3.7/site-packages/setuptools/_vendor/importlib_metadata/__init__.py", line 1003, in entry_points
    return SelectableGroups.load(eps).select(**params)
  File "/root/git/empty_test/venv/lib/python3.7/site-packages/setuptools/_vendor/importlib_metadata/__init__.py", line 453, in load
    ordered = sorted(eps, key=by_group)
  File "/root/git/empty_test/venv/lib/python3.7/site-packages/setuptools/_vendor/importlib_metadata/__init__.py", line 1001, in <genexpr>
    dist.entry_points for dist in unique(distributions())
  File "/root/git/empty_test/venv/lib/python3.7/site-packages/setuptools/_vendor/importlib_metadata/_itertools.py", line 16, in unique_everseen
    k = key(element)
AttributeError: 'PathDistribution' object has no attribute '_normalized_name'
```


Discussion:
I'm very sorry if this belongs to nox or virtualenv, but I have no clue where to start
I have the same issue, the setup is failing python 3.7.1 with poetry 
Same issue, running Python 3.7.3  and poetry@latest.

Downgrade to `setuptools==60.8.2` fix the issue.
I'm also encountering this when using the latest `virtualenv` with `setuptools==60.9.0` on Python 3.7. No issues on later Python versions. Let me know if more information would be helpful!
I was able to replicate the error with these steps:

```
draft $ python3.9 -m venv env
draft $ env/bin/pip install -q -U pip setuptools
draft $ env/bin/pip install importlib_metadata==4.2.0
draft $ env/bin/python
Python 3.9.6 (v3.9.6:db3ff76da1, Jun 28 2021, 11:14:58) 
[Clang 12.0.5 (clang-1205.0.22.9)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import importlib_metadata
>>> from setuptools._importlib import metadata as md
>>> md.entry_points(group='distutils.setup_keywords')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/jaraco/draft/env/lib/python3.9/site-packages/setuptools/_vendor/importlib_metadata/__init__.py", line 1003, in entry_points
    return SelectableGroups.load(eps).select(**params)
  File "/Users/jaraco/draft/env/lib/python3.9/site-packages/setuptools/_vendor/importlib_metadata/__init__.py", line 453, in load
    ordered = sorted(eps, key=by_group)
  File "/Users/jaraco/draft/env/lib/python3.9/site-packages/setuptools/_vendor/importlib_metadata/__init__.py", line 1000, in <genexpr>
    eps = itertools.chain.from_iterable(
  File "/Users/jaraco/draft/env/lib/python3.9/site-packages/setuptools/_vendor/importlib_metadata/_itertools.py", line 16, in unique_everseen
    k = key(element)
AttributeError: 'PathDistribution' object has no attribute '_normalized_name'
```

The issue is that `importlib_metadata` is expected to exist at exactly one version, but now that there's a vendored copy in Setuptools, one could encounter multiple, perhaps incompatible versions. The `Distribution._normalized_name` property was added in `importlib_metadata` 4.3, so the presence of `importlib_metadata` 4.2 causes its [MetadataPathFinder to be installed](https://github.com/python/importlib_metadata/blob/2ef0770570716e932dd4f3fec043db5a3398dc42/importlib_metadata/__init__.py#L869-L870) and thus for `Distribution` objects without `_normalized_name` property to be provided.

It's not yet obvious to me what the right fix is going to be.

I would not expect the issue to affect Python 3.10 or later, as in that case, vendored importlib_metadata is not used.
I'm very sorry if this belongs to nox or virtualenv, but I have no clue where to start
I have the same issue, the setup is failing python 3.7.1 with poetry 
Same issue, running Python 3.7.3  and poetry@latest.

Downgrade to `setuptools==60.8.2` fix the issue.
I'm also encountering this when using the latest `virtualenv` with `setuptools==60.9.0` on Python 3.7. No issues on later Python versions. Let me know if more information would be helpful!
I was able to replicate the error with these steps:

```
draft $ python3.9 -m venv env
draft $ env/bin/pip install -q -U pip setuptools
draft $ env/bin/pip install importlib_metadata==4.2.0
draft $ env/bin/python
Python 3.9.6 (v3.9.6:db3ff76da1, Jun 28 2021, 11:14:58) 
[Clang 12.0.5 (clang-1205.0.22.9)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import importlib_metadata
>>> from setuptools._importlib import metadata as md
>>> md.entry_points(group='distutils.setup_keywords')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/jaraco/draft/env/lib/python3.9/site-packages/setuptools/_vendor/importlib_metadata/__init__.py", line 1003, in entry_points
    return SelectableGroups.load(eps).select(**params)
  File "/Users/jaraco/draft/env/lib/python3.9/site-packages/setuptools/_vendor/importlib_metadata/__init__.py", line 453, in load
    ordered = sorted(eps, key=by_group)
  File "/Users/jaraco/draft/env/lib/python3.9/site-packages/setuptools/_vendor/importlib_metadata/__init__.py", line 1000, in <genexpr>
    eps = itertools.chain.from_iterable(
  File "/Users/jaraco/draft/env/lib/python3.9/site-packages/setuptools/_vendor/importlib_metadata/_itertools.py", line 16, in unique_everseen
    k = key(element)
AttributeError: 'PathDistribution' object has no attribute '_normalized_name'
```

The issue is that `importlib_metadata` is expected to exist at exactly one version, but now that there's a vendored copy in Setuptools, one could encounter multiple, perhaps incompatible versions. The `Distribution._normalized_name` property was added in `importlib_metadata` 4.3, so the presence of `importlib_metadata` 4.2 causes its [MetadataPathFinder to be installed](https://github.com/python/importlib_metadata/blob/2ef0770570716e932dd4f3fec043db5a3398dc42/importlib_metadata/__init__.py#L869-L870) and thus for `Distribution` objects without `_normalized_name` property to be provided.

It's not yet obvious to me what the right fix is going to be.

I would not expect the issue to affect Python 3.10 or later, as in that case, vendored importlib_metadata is not used.

Resolving patch:
diff --git a/setuptools/_importlib.py b/setuptools/_importlib.py
--- a/setuptools/_importlib.py
+++ b/setuptools/_importlib.py
@@ -1,8 +1,31 @@
 import sys
 
 
+def disable_importlib_metadata_finder(metadata):
+    """
+    Ensure importlib_metadata doesn't provide older, incompatible
+    Distributions.
+
+    Workaround for #3102.
+    """
+    try:
+        import importlib_metadata
+    except ImportError:
+        return
+    if importlib_metadata is metadata:
+        return
+    to_remove = [
+        ob
+        for ob in sys.meta_path
+        if isinstance(ob, importlib_metadata.MetadataPathFinder)
+    ]
+    for item in to_remove:
+        sys.meta_path.remove(item)
+
+
 if sys.version_info < (3, 10):
     from setuptools.extern import importlib_metadata as metadata
+    disable_importlib_metadata_finder(metadata)
 else:
     import importlib.metadata as metadata  # noqa: F401
 


Test patch: