setuptools
- Python course
- Advanced
- Lesson 74/83
- 5 min
Learn packaging with setuptools.
setuptools
setuptools is a tool that builds Python packages and uploads them to PyPI. In modern usage it is not called directly; it is declared in the [build-system] table inside pyproject.toml.
[build-system]
requires = ["setuptools>=61"]
build-backend = "setuptools.build_meta"requires lists the packages needed to build; build-backend names the backend to run. This tells pip install which tool to use.
The [build-system] table
The [build-system] table has two main fields. requires is the list of packages installed during build; build-backend is the dotted path to the Python object that drives the build.
[build-system]
requires = ["setuptools>=61"]
build-backend = "setuptools.build_meta"The setuptools build_meta backend builds a wheel from source. Without this configuration pip install would not know which tool to call.
Practice Read the build-backend with tomllib
Start with import tomllib. Convert the TOML string below into structured data with tomllib.loads(...) and assign it to data:
[build-system]
requires = ["setuptools>=61"]
build-backend = "setuptools.build_meta"Then print the backend path with print(data["build-system"]["build-backend"]).
Starter code
# bring in the tomllib module
# load the build-system table as structured data
# print the build-backend fieldSample solution
import tomllib
data = tomllib.loads('''
[build-system]
requires = ["setuptools>=61"]
build-backend = "setuptools.build_meta"
''')
print(data["build-system"]["build-backend"])Expected output
setuptools.build_meta
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Complete the build-backend line
[build-system]
requires = ["setuptools>=61"]
____ = "setuptools.build_meta"
[build-system]
requires = ["setuptools>=61"]
build-backend = "setuptools.build_meta"
Explanation: build-backend names the build backend.
2 What does setuptools.build_meta do?
- Uploads the package to PyPI
- Builds a wheel from source Correct answer
- Creates a virtual environment
- Freezes dependencies
Explanation: build_meta builds a wheel from source.
3 Match the setuptools concepts
| Term | Meaning |
|---|---|
setuptools |
Build tool |
build_meta |
Produces a wheel |
requires |
Build packages |
wheel |
Installable artifact |