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 pep517 if setup.py is present and setuptools cannot be imported #10717

Merged
merged 5 commits into from
Apr 23, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/10717.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fallback to PEP 517 if ``setup.py`` is present in a project, but ``setuptools`` cannot be imported
pradyunsg marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 7 additions & 0 deletions src/pip/_internal/cli/cmdoptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -775,6 +776,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

Expand Down
11 changes: 9 additions & 2 deletions src/pip/_internal/pyproject.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import importlib.util
import os
from collections import namedtuple
from typing import Any, List, Optional
Expand Down Expand Up @@ -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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the setuptools check can be technically made much sooner in this function, but this way, it is avoided when not necessary. Not sure which approach is better.


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