Install pep 518 build system requirements only (to build sdist)

Viewed 1138

PEP 518 introduced the pyproject.toml file, as well as a section describing the tools needed to build:

[build-system]
requires = ["setuptools", "wheel", "numpy>=1.13"]

Here, I'm telling the build system (implicitly setuptools) that I need these three requirements installed before I can run the build. (And yes, I actually do need numpy as part of the build process.)

When I run pip wheel, it knows to look for this section in this file, install the requirements, and then build the wheel. But pip has no way to create an sdist distribution (and its maintainers seem reluctant to add one), so I need to run python setup.py sdist. And that's where the problem comes in: setup.py doesn't know it needs numpy, and the build fails.

Is there a standard way to just install the requirements, and then build an sdist? In particular, pip has moved toward build isolation, so can this be done with isolation? Failing that, I could create my own environment for some isolation; then, what's the best way to install the requirements in some environment?

2 Answers

One way is with the pypa project pep517 (though the module is marked "experimental")

here's a sample dist with a special dependency that I tried:

# setup.py
from setuptools import setup
import astpretty
setup(name='wat', version='1')
# pyproject.toml
[build-system]
requires = ["setuptools", "wheel", "astpretty"]
build-backend = "setuptools.build_meta"
$ python -m pep517.build --source .
WARNING: You are using pip version 20.2.1; however, version 20.2.2 is available.
You should consider upgrading via the '/tmp/x/venv/bin/python -m pip install --upgrade pip' command.
running egg_info
creating wat.egg-info
writing wat.egg-info/PKG-INFO
writing dependency_links to wat.egg-info/dependency_links.txt
writing top-level names to wat.egg-info/top_level.txt
writing manifest file 'wat.egg-info/SOURCES.txt'
reading manifest file 'wat.egg-info/SOURCES.txt'
writing manifest file 'wat.egg-info/SOURCES.txt'
running sdist
running egg_info
writing wat.egg-info/PKG-INFO
writing dependency_links to wat.egg-info/dependency_links.txt
writing top-level names to wat.egg-info/top_level.txt
reading manifest file 'wat.egg-info/SOURCES.txt'
writing manifest file 'wat.egg-info/SOURCES.txt'
warning: sdist: standard file not found: should have one of README, README.rst, README.txt, README.md

running check
warning: check: missing required meta-data: url

warning: check: missing meta-data: either (author and author_email) or (maintainer and maintainer_email) must be supplied

creating wat-1
creating wat-1/wat.egg-info
copying files to wat-1...
copying pyproject.toml -> wat-1
copying setup.py -> wat-1
copying wat.egg-info/PKG-INFO -> wat-1/wat.egg-info
copying wat.egg-info/SOURCES.txt -> wat-1/wat.egg-info
copying wat.egg-info/dependency_links.txt -> wat-1/wat.egg-info
copying wat.egg-info/top_level.txt -> wat-1/wat.egg-info
Writing wat-1/setup.cfg
Creating tar archive
removing 'wat-1' (and everything under it)
$ ls dist/
wat-1.tar.gz

You want build. It is project under the Python Package Authority (PyPA) that was created specifically to build PEP 517 packages.

It will build both a wheel and an sdist. However you can tell it to only build an sdist as well.

Related