From 2af305cdcf6dbad48930f010da40df940c4dfc7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Hrn=C4=8Diar?= Date: Wed, 8 Dec 2021 15:36:30 +0100 Subject: [PATCH] Fallback to pep517 if setup.py is present and setuptools cannot be imported --- src/pip/_internal/cli/cmdoptions.py | 8 ++++++++ src/pip/_internal/pyproject.py | 11 +++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/pip/_internal/cli/cmdoptions.py b/src/pip/_internal/cli/cmdoptions.py index 11ddc610c14..f448f3162cc 100644 --- a/src/pip/_internal/cli/cmdoptions.py +++ b/src/pip/_internal/cli/cmdoptions.py @@ -13,6 +13,7 @@ import os import textwrap import warnings +import importlib.util from functools import partial from optparse import SUPPRESS_HELP, Option, OptionGroup, OptionParser, Values from textwrap import dedent @@ -774,6 +775,13 @@ def _handle_no_use_pep517( """ raise_option_error(parser, option=option, msg=msg) + # If user doesn't wish to use pep517, we check if setuptools is installed + # and raise error if it is not. + if not importlib.util.find_spec("setuptools"): + msg = """It is not possible to use --no-use-pep517 without setuptools + installed.""" + raise_option_error(parser, option=option, msg=msg) + # Otherwise, --no-use-pep517 was passed via the command-line. parser.values.use_pep517 = False diff --git a/src/pip/_internal/pyproject.py b/src/pip/_internal/pyproject.py index 0607b048bae..6da46613ac6 100644 --- a/src/pip/_internal/pyproject.py +++ b/src/pip/_internal/pyproject.py @@ -1,4 +1,5 @@ import os +import importlib.util from collections import namedtuple from typing import Any, List, Optional @@ -89,9 +90,15 @@ def load_pyproject_toml( # If we haven't worked out whether to use PEP 517 yet, # and the user hasn't explicitly stated a preference, - # we do so if the project has a pyproject.toml file. + # we do so if the project has a pyproject.toml file + # or if we cannot import setuptools. + + # We fallback to PEP 517 when without setuptools, + # so setuptools can be installed as a default build backend. + # For more info see: + # https://discuss.python.org/t/pip-without-setuptools-could-the-experience-be-improved/11810/9 elif use_pep517 is None: - use_pep517 = has_pyproject + use_pep517 = has_pyproject or not importlib.util.find_spec("setuptools") # At this point, we know whether we're going to use PEP 517. assert use_pep517 is not None