Fallback to pep517 if setup.py is present and setuptools cannot be imported

This commit is contained in:
Tomáš Hrnčiar 2022-04-26 12:26:29 +02:00
parent 1963d79695
commit 5a8c8943b0
2 changed files with 85 additions and 1 deletions

76
452d7da880.patch Normal file
View File

@ -0,0 +1,76 @@
From 452d7da8801ca318f280bd1c1085c60b0b6f747f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Hrn=C4=8Diar?= <thrnciar@redhat.com>
Date: Sat, 23 Apr 2022 16:56:59 +0200
Subject: [PATCH] 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 <uranusjr@gmail.com>
Co-authored-by: Pradyun Gedam <pradyunsg@gmail.com>
---
news/10717.bugfix.rst | 1 +
src/pip/_internal/cli/cmdoptions.py | 7 +++++++
src/pip/_internal/pyproject.py | 11 +++++++++--
3 files changed, 17 insertions(+), 2 deletions(-)
create mode 100644 news/10717.bugfix.rst
diff --git a/news/10717.bugfix.rst b/news/10717.bugfix.rst
new file mode 100644
index 00000000000..950a4521763
--- /dev/null
+++ b/news/10717.bugfix.rst
@@ -0,0 +1 @@
+Fallback to pyproject.toml-based builds if ``setup.py`` is present in a project, but ``setuptools`` cannot be imported.
diff --git a/src/pip/_internal/cli/cmdoptions.py b/src/pip/_internal/cli/cmdoptions.py
index cd1e8a00f63..91b94e3028c 100644
--- a/src/pip/_internal/cli/cmdoptions.py
+++ b/src/pip/_internal/cli/cmdoptions.py
@@ -10,6 +10,7 @@
# The following comment should be removed at some point in the future.
# mypy: strict-optional=False
+import importlib.util
import logging
import os
import textwrap
@@ -770,6 +771,12 @@ 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 e183eaf8658..1e9119f3e5c 100644
--- a/src/pip/_internal/pyproject.py
+++ b/src/pip/_internal/pyproject.py
@@ -1,3 +1,4 @@
+import importlib.util
import os
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

View File

@ -21,7 +21,7 @@
Name: python-%{srcname}
Version: %{base_version}%{?prerel:~%{prerel}}
Release: 1%{?dist}
Release: 2%{?dist}
Summary: A tool for installing and managing Python packages
# We bundle a lot of libraries with pip, which itself is under MIT license.
@ -89,6 +89,10 @@ Patch: nowarn-pip._internal.main.patch
# Upstream issue: https://github.com/pypa/packaging/issues/368
Patch: no-version-warning.patch
# Fallback to pep517 if setup.py is present and setuptools cannot be imported
# https://bugzilla.redhat.com/show_bug.cgi?id=2020635
Patch: https://github.com/pypa/pip/commit/452d7da880.patch
# Downstream only patch
# Users might have local installations of pip from using
# `pip install --user --upgrade pip` on older/newer versions.
@ -407,6 +411,10 @@ pytest_k='not completion and
%{python_wheel_dir}/%{python_wheel_name}
%changelog
* Tue Apr 26 2022 Tomáš Hrnčiar <thrnciar@redhat.com> - 22.0.4-2
- Fallback to pep517 if setup.py is present and setuptools cannot be imported
- Fixes: rhbz#2020635
* Mon Mar 21 2022 Karolina Surma <ksurma@redhat.com> - 22.0.4-1
- Update to 22.0.4
Resolves: rhbz#2061262