Repository: pypa/setuptools

Issue:
AttributeError: module '_distutils_hack' has no attribute 'ensure_shim'
### setuptools version

setuptools==60.0.5

### Python version

python 3.10

### OS

macOS intel 12.1

### Additional environment information

_No response_

### Description

python3 -m pip install  -U setuptools

### Expected behavior

python3 -m pip install  -U setuptools

### How to Reproduce

brew install python@3.10
python3 -m pip install  -U setuptools

### Output

```
Collecting setuptools
  Downloading setuptools-60.1.1-py3-none-any.whl (952 kB)
     |████████████████████████████████| 952 kB 351 kB/s            
Installing collected packages: setuptools
  Attempting uninstall: setuptools
    Found existing installation: setuptools 60.0.5
    Uninstalling setuptools-60.0.5:
      Successfully uninstalled setuptools-60.0.5
Successfully installed setuptools-60.1.1
```

and when you exit and relogin

```
Error processing line 1 of /usr/local/Cellar/python@3.10/3.10.1/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/distutils-precedence.pth:

  Traceback (most recent call last):
    File "/usr/local/Cellar/python@3.10/3.10.1/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site.py", line 186, in addpackage
      exec(line)
    File "<string>", line 1, in <module>
  AttributeError: module '_distutils_hack' has no attribute 'ensure_shim'

Remainder of file ignored
```

### Code of Conduct

- [X] I agree to follow the PSF Code of Conduct
AttributeError: module '_distutils_hack' has no attribute 'ensure_shim'
### setuptools version

setuptools==60.0.5

### Python version

python 3.10

### OS

macOS intel 12.1

### Additional environment information

_No response_

### Description

python3 -m pip install  -U setuptools

### Expected behavior

python3 -m pip install  -U setuptools

### How to Reproduce

brew install python@3.10
python3 -m pip install  -U setuptools

### Output

```
Collecting setuptools
  Downloading setuptools-60.1.1-py3-none-any.whl (952 kB)
     |████████████████████████████████| 952 kB 351 kB/s            
Installing collected packages: setuptools
  Attempting uninstall: setuptools
    Found existing installation: setuptools 60.0.5
    Uninstalling setuptools-60.0.5:
      Successfully uninstalled setuptools-60.0.5
Successfully installed setuptools-60.1.1
```

and when you exit and relogin

```
Error processing line 1 of /usr/local/Cellar/python@3.10/3.10.1/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/distutils-precedence.pth:

  Traceback (most recent call last):
    File "/usr/local/Cellar/python@3.10/3.10.1/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site.py", line 186, in addpackage
      exec(line)
    File "<string>", line 1, in <module>
  AttributeError: module '_distutils_hack' has no attribute 'ensure_shim'

Remainder of file ignored
```

### Code of Conduct

- [X] I agree to follow the PSF Code of Conduct


Discussion:
i tested the version v60.1.1 and v60.1.0,both same error,only  setuptools==60.0.5 can work
Setuptools 60.1 ships with a new `_distutils_hack` module that does contain that attribute. For some reason, in your environment, there's another `_distutils_hack` module that's taking precedence, probably from an older Setuptools.

You should be able to run `python3.10 -c "import _distutils_hack; print(_distutils_hack.__file__)"` to see where that module can be found. Can you do that please and report the output? My guess is you have a user-installed copy in `~/.local`.

In theory, loading the shim should be tolerant to configurations like these. In #2962 and #2964, we're exploring some approaches that could limit which .pth files invoke which _hack modules.
This issue is likely another manifestation of #2957.
Although the issues are similar, the more selective fix for that issue will likely not fix this one, so I'll deal with it separately.
i tested the version v60.1.1 and v60.1.0,both same error,only  setuptools==60.0.5 can work
Setuptools 60.1 ships with a new `_distutils_hack` module that does contain that attribute. For some reason, in your environment, there's another `_distutils_hack` module that's taking precedence, probably from an older Setuptools.

You should be able to run `python3.10 -c "import _distutils_hack; print(_distutils_hack.__file__)"` to see where that module can be found. Can you do that please and report the output? My guess is you have a user-installed copy in `~/.local`.

In theory, loading the shim should be tolerant to configurations like these. In #2962 and #2964, we're exploring some approaches that could limit which .pth files invoke which _hack modules.
This issue is likely another manifestation of #2957.
Although the issues are similar, the more selective fix for that issue will likely not fix this one, so I'll deal with it separately.

Resolving patch:
diff --git a/_distutils_hack/__init__.py b/_distutils_hack/__init__.py
--- a/_distutils_hack/__init__.py
+++ b/_distutils_hack/__init__.py
@@ -136,20 +136,20 @@ def frame_file_is_setup(frame):
 DISTUTILS_FINDER = DistutilsMetaFinder()
 
 
-def ensure_shim():
-    DISTUTILS_FINDER in sys.meta_path or add_shim()
+def add_shim():
+    DISTUTILS_FINDER in sys.meta_path or insert_shim()
 
 
 @contextlib.contextmanager
 def shim():
-    add_shim()
+    insert_shim()
     try:
         yield
     finally:
         remove_shim()
 
 
-def add_shim():
+def insert_shim():
     sys.meta_path.insert(0, DISTUTILS_FINDER)
 
 
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -55,7 +55,7 @@ class install_with_pth(install):
         import os
         var = 'SETUPTOOLS_USE_DISTUTILS'
         enabled = os.environ.get(var, 'local') == 'local'
-        enabled and __import__('_distutils_hack').ensure_shim()
+        enabled and __import__('_distutils_hack').add_shim()
         """).lstrip().replace('\n', '; ')
 
     def initialize_options(self):


Test patch: