Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback to stdlib distutils if setuptools._distutils is missing #4441

Closed

Conversation

Avasam
Copy link
Contributor

@Avasam Avasam commented Jun 26, 2024

Summary of changes

If setuptools._distutils is missing and SETUPTOOLS_USE_DISTUTILS environment variable isn't set to stdlib, importing setuptools will fail.
This change fallsback to stdlib despite SETUPTOOLS_USE_DISTUTILS=local if setuptools._distutils is missing and shows a warning for Python 3.11 and below.

To test this, simply delete or rename setuptools._distutils (either with an editable install or directly in your site-packages) and set SETUPTOOLS_USE_DISTUTILS to nothing or local. I'd like to write a test for this and may need some pointers to get started since this test would include deleting files (or at least making an import inaccessible)

Before:

>>> import setuptools
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "E:\Users\Avasam\Documents\Git\setuptools\setuptools\__init__.py", line 8, in <module>
    import _distutils_hack.override  # noqa: F401
  File "E:\Users\Avasam\Documents\Git\setuptools\_distutils_hack\override.py", line 1, in <module>
    __import__('_distutils_hack').do_override()
  File "E:\Users\Avasam\Documents\Git\setuptools\_distutils_hack\__init__.py", line 89, in do_override
    ensure_local_distutils()
  File "E:\Users\Avasam\Documents\Git\setuptools\_distutils_hack\__init__.py", line 76, in ensure_local_distutils
    assert '_distutils' in core.__file__, core.__file__
AssertionError: C:\Program Files\Python39\lib\distutils\core.py

After:

>>> import setuptools
E:\Users\Avasam\Documents\Git\setuptools\_distutils_hack\__init__.py:66: UserWarning: environment variable SETUPTOOLS_USE_DISTUTILS is set to 'local' or None, but `setuptools._distutils` could not be imported. Falling back to stdlib distutils. This will lead to an error in Python 3.12 and above.
  warnings.warn(

Closes #4439
The validity of this fix assumes that the user didn't themselves delete setuptool._distutils (messing up their own installation) and that distributing setuptools without setuptools._distutils and w/o setting an environment variable is a valid use-case. (sounds valid to me for vendor-stripped setuptools)

Pull Request Checklist

@jaraco
Copy link
Member

jaraco commented Jun 29, 2024

I really don't want to open up this code path. Since Setuptools adopted distutils, it's been primarily reliant on local distutils and designed only to fall back to stdlib if explicitly indicated by the user. This approach requires the user to have ownership over that fallback, and it's important for them to have ownership as we work to deprecate that path altogether (#4137).

@jaraco jaraco closed this Jun 29, 2024
@Avasam Avasam deleted the stdlib-fallback-if-_disturils-is-missing branch June 29, 2024 20:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AssertionError importing setuptools
2 participants