Sub topic 8
Including data files and license files
This chapter assumes you can read a pyproject.toml, have built a wheel with python -m build, and know that a wheel is a zip archive you can open. It covers two things that often go missing from a build: non-Python files inside your package, and license files.
Why files go missing
A wheel contains Python modules by default. Other files in your package directory, such as a JSON file, a template, or a text list, are not always copied. A test run from the source tree passes because the file is on disk. The installed package then fails with FileNotFoundError.
Always check the built wheel, not the working directory.
Declaring package data
Suppose greeter ships a list of greetings in src/greeter/data/greetings.txt. Tell setuptools to include it:
[tool.setuptools.package-data]
greeter = ["data/*.txt"]The key is the import package name. The patterns are relative to that package's directory. Recent setuptools versions with a pyproject.toml also include data files that are tracked in the package directory by default (include-package-data defaults to true). Rely on that default only if you have verified it in the built wheel. Explicit patterns are easier to reason about.
Keep data files inside the package directory. Files outside src/greeter/ cannot be reached after installation.
Reading data at runtime
Do not build paths with __file__. That breaks when a package is installed as a zip or in other unusual layouts. Use importlib.resources:
# src/greeter/messages.py
from importlib.resources import files
def load_greetings() -> list[str]:
text = (files("greeter") / "data" / "greetings.txt").read_text(encoding="utf-8")
return [line for line in text.splitlines() if line]This uses the list[str] annotation, so it needs Python 3.9 or newer, which matches requires-python = ">=3.9" in this project. files() is available from Python 3.9.
License files
The [project] table can name the license and the files that hold its text:
[project]
license = "MIT"
license-files = ["LICENSE"]license is an SPDX expression, a standard short identifier such as MIT or Apache-2.0. license-files lists glob patterns for the files to copy into the built artifacts. Both the sdist and the wheel carry them, and the wheel stores them under *.dist-info/licenses/. This form needs setuptools>=77, which is why the build-system table pins that version.
Many licenses require that the text travel with every copy of the code. Including the file is a legal duty in those cases, not a courtesy.
Checking the result
List the wheel contents and look for your data file and license:
python -m build
python -m zipfile -l dist/greeter-0.1.0-py3-none-any.whlYou should see greeter/data/greetings.txt and greeter-0.1.0.dist-info/licenses/LICENSE in the output. If either is absent, fix the configuration and rebuild.
Common errors
- File works in development, missing after install: the pattern in
package-datadoes not match, or the file sits outside the package directory. - Pattern matches nothing silently: build tools usually do not warn. Verify with the zip listing.
- Wrong key in
package-data: it must be the import package name (greeter), not the project name on PyPI, though here they happen to match. - Build error about license fields: an older setuptools does not understand the SPDX
licensestring. Raise therequirespin.
Exercise
Your tests pass from the source tree, but after pip install of the wheel, load_greetings() raises FileNotFoundError. Name two things to check and the command that shows what the wheel contains.
Show answer
Check that package-data lists a pattern matching the file (such as data/*.txt under the key greeter) and that the file is inside src/greeter/. Run python -m zipfile -l dist/greeter-0.1.0-py3-none-any.whl to list the wheel contents and confirm the file is present.