Skip to content

Commit 452d7da

Browse files
hrnciaruranusjrpradyunsg
authored
Fallback to pyproject.toml-based builds if setuptools cannot be imported (#10717)
This fallback is only triggered if the project has a `setup.py` file. Co-authored-by: Tzu-ping Chung <[email protected]> Co-authored-by: Pradyun Gedam <[email protected]>
1 parent 5570b42 commit 452d7da

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

news/10717.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fallback to pyproject.toml-based builds if ``setup.py`` is present in a project, but ``setuptools`` cannot be imported.

src/pip/_internal/cli/cmdoptions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
# The following comment should be removed at some point in the future.
1111
# mypy: strict-optional=False
1212

13+
import importlib.util
1314
import logging
1415
import os
1516
import textwrap
@@ -770,6 +771,12 @@ def _handle_no_use_pep517(
770771
"""
771772
raise_option_error(parser, option=option, msg=msg)
772773

774+
# If user doesn't wish to use pep517, we check if setuptools is installed
775+
# and raise error if it is not.
776+
if not importlib.util.find_spec("setuptools"):
777+
msg = "It is not possible to use --no-use-pep517 without setuptools installed."
778+
raise_option_error(parser, option=option, msg=msg)
779+
773780
# Otherwise, --no-use-pep517 was passed via the command-line.
774781
parser.values.use_pep517 = False
775782

src/pip/_internal/pyproject.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import importlib.util
12
import os
23
from collections import namedtuple
34
from typing import Any, List, Optional
@@ -89,9 +90,15 @@ def load_pyproject_toml(
8990

9091
# If we haven't worked out whether to use PEP 517 yet,
9192
# and the user hasn't explicitly stated a preference,
92-
# we do so if the project has a pyproject.toml file.
93+
# we do so if the project has a pyproject.toml file
94+
# or if we cannot import setuptools.
95+
96+
# We fallback to PEP 517 when without setuptools,
97+
# so setuptools can be installed as a default build backend.
98+
# For more info see:
99+
# https://discuss.python.org/t/pip-without-setuptools-could-the-experience-be-improved/11810/9
93100
elif use_pep517 is None:
94-
use_pep517 = has_pyproject
101+
use_pep517 = has_pyproject or not importlib.util.find_spec("setuptools")
95102

96103
# At this point, we know whether we're going to use PEP 517.
97104
assert use_pep517 is not None

0 commit comments

Comments
 (0)