Skip to content

[BUG] distutils cannot be imported on Python 3.12 (without distutils) if pip was imported #3661

Closed
@vstinner

Description

@vstinner

setuptools version

65.5.0

Python version

3.12 (dev, without distutils)

OS

Linux

Additional environment information

No response

Description

I just removed the distutils packages from Python 3.12: commit. setuptools works as expected, it provides its copy of distutils. Good.

But if pip is imported first, import setuptools or import distutils fails on import setuptools, since _distutils_hack has a special path when pip is installed which requires to get the stdlib distutils... which no longer exists.

Extract of _distutils_hack/__init__.py:

    def spec_for_pip(self):
        """
        Ensure stdlib distutils when running under pip.
        See pypa/pip#8761 for rationale.
        """

See pypa/pip#8761 for the rationale.

Well, this special case should be revisited for Python >= 3.12.0 alpha 2.

Expected behavior

Importing setuptools on Python 3.12 after pip was imported should just work.

How to Reproduce

$ python3.12
Python 3.12.0a1+ (heads/main:f09da28768, Nov  4 2022, 17:46:22) [GCC 12.2.1 20220819 (Red Hat 12.2.1-2)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pip
>>> import setuptools

Output

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/py312/lib/python3.12/site-packages/setuptools/__init__.py", line 8, in <module>
    import _distutils_hack.override  # noqa: F401
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/py312/lib/python3.12/site-packages/_distutils_hack/override.py", line 1, in <module>
    __import__('_distutils_hack').do_override()
  File "/opt/py312/lib/python3.12/site-packages/_distutils_hack/__init__.py", line 77, in do_override
    ensure_local_distutils()
  File "/opt/py312/lib/python3.12/site-packages/_distutils_hack/__init__.py", line 60, in ensure_local_distutils
    importlib.import_module('distutils')
  File "/opt/py312/lib/python3.12/importlib/__init__.py", line 124, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'distutils'

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs TriageIssues that need to be evaluated for severity and status.bug

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions