pypa / pypa/setuptools

[BUG] DistutilsMetaFinder breaks distutils submodule imports when old setuptools is higher on path

Open
#3,073 5 comments 0 reactions 1 assignee View on GitHub

@jaraco is already working on this.

Since Feb 11, 2022.

bug Needs Implementation
Dominant language
Python
Stars
2.9k
Forks
1.4k
Avg merge
1d 1h
Merged PRs (30d)
1

Description

setuptools version

setuptools====60.7.0

Python version

Python 3.7

OS

macOS

Additional environment information

Similar to #2957, this occurs when an old setuptools is on path but a newer setuptools (with a distutils shimming .pth file) is installed.

Description

This is a followup to #2957 where the .pth shim broke distutils imports when an older setuptools is higher on path. The fix in #2962 allows distutils to be imported as well as submodules, but those submodules are not usable.

I'm running into this issue when running a PEX which contains an older version of setuptools but the Python interpreter has a newer version of setuptools. In the bootstrap phase pex removes the new setuptools from sys.path and adds the older setuptools. DistutilsMetaFinder is not removed from sys.meta_path during this phase and causes distutils submodules to fail.

Expected behavior

The presence of a new-but-not-loadable setuptools on sys.path doesn't break distutils submodules.

How to Reproduce

As in #2957 a reproducer is:

python3 -m venv /tmp/bang && source /tmp/bang/bin/activate  # give us a clean venv to work in
python -m pip install 'setuptools>60'  # install new setuptools to venv site-packages
python -m pip install 'setuptools<45' -t .  # install old setuptools to cwd- higher than site-packages on path for most systems
python -c "import distutils.util; print(distutils.util)"
Output
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: module 'distutils' has no attribute 'util'

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.