Sub topic 8
Including data files and license files
A wheel contains only what the build backend decides to put in it. Python modules are picked up automatically. Other files, such as templates, JSON lookup tables, and your license text, are not always picked up. This chapter covers how to include them, how to read them at runtime, and how to confirm they reached the built artifacts.
This chapter assumes you have the greeter project from the earlier chapters. It has a src layout, a pyproject.toml using setuptools as the build backend, and the ability to run python -m build. Some of the settings below are setuptools-specific. Other backends use different option names, though the ideas carry over.
Two kinds of non-code files
The two kinds are handled differently:
- Package data is a file your code reads at runtime, for example a message template. It must be installed next to the code, inside the package directory.
- License files are legal documents. They are not read by your code, but they should travel with every distribution so recipients know their rights.
Both can be missing from a wheel without any build error. The failure appears later, as a FileNotFoundError on a user's machine or a package with no license text.
Where the build puts each file
The sdist (source distribution) and the wheel are built from the same source tree, but they select files with different rules.
The consequence is that a file can be present in the sdist and absent from the wheel. Most users install the wheel, so the wheel is what you need to check.
Adding a data file to the package
Create a data directory inside the import package, not at the project root. Files outside src/greeter/ are not installed into site-packages.
src/greeter/
├── __init__.py
├── cli.py
├── templates.py
├── py.typed
└── data/
└── greeting.txtPut one line in src/greeter/data/greeting.txt:
Hello, {name}! Welcome aboard.Next, tell setuptools to include it:
[tool.setuptools.package-data]
greeter = ["data/*.txt"]The key is the package name. The patterns are relative to that package's directory, so data/*.txt means src/greeter/data/*.txt. Patterns use simple globbing and are not full regular expressions.
The include-package-data default
When you configure setuptools through pyproject.toml, include-package-data defaults to true. That setting also includes files that are listed in MANIFEST.in or tracked by a version-control plugin such as setuptools-scm. It does not include every file in the package directory. A file that is only untracked on disk and matched by neither mechanism is skipped. Declaring explicit package-data patterns is the most predictable approach, because it does not depend on Git state or a separate manifest file.
Reading the file at runtime
Do not open data files with a path built from __file__ or the current working directory. That breaks when the package is installed as a zip or run from another directory. Use importlib.resources, which finds files relative to the installed package.
# src/greeter/templates.py
from importlib.resources import files
def load_template(name: str = "greeting.txt") -> str:
"""Return the text of a bundled template file."""
resource = files("greeter") / "data" / name
return resource.read_text(encoding="utf-8")
def render(name: str) -> str:
return load_template().format(name=name)files() returns a Traversable object. You navigate it with / and read it with read_text() or read_bytes(). This API is available from Python 3.9, which matches requires-python = ">=3.9" in the earlier pyproject.toml. Passing the encoding explicitly avoids platform-dependent defaults.
Test it in an installed environment, not in the source tree:
python -m pip install .
python -c "from greeter.templates import render; print(render('Ada'))"The output should be Hello, Ada! Welcome aboard.. If you run the same command against an editable install (pip install -e .), it can succeed even when the wheel would be missing the file. Editable installs point at your source directory. The wheel check below catches this class of bug.
License files
The project table in the earlier pyproject.toml already declares the license:
[project]
license = "MIT"
license-files = ["LICENSE"]licenseis an SPDX license expression, a standard short identifier such asMIT,Apache-2.0, orMIT OR Apache-2.0.license-filesis a list of glob patterns, relative topyproject.toml, naming the files that contain the license text.
This form follows PEP 639 and needs setuptools>=77, which is why the build-system table requires that version. With an older setuptools, the build fails or ignores these keys. Older tutorials use license = {text = "MIT"} or license = {file = "LICENSE"}, and a table-valued license is now deprecated. Classifiers beginning License :: are also deprecated when you use a license expression.
If you omit license-files, setuptools looks for default patterns: LICEN[CS]E*, COPYING*, NOTICE*, and AUTHORS*. Listing the file explicitly makes the intent visible and the build fail loudly if the file is missing.
In the sdist, the license files sit at the top level of the archive. In the wheel, they are placed under greeter-0.1.0.dist-info/licenses/.
Verifying what was built
After building, list the contents of both artifacts. A wheel is a zip archive, and an sdist is a gzipped tar archive.
python -m build
python -m zipfile -l dist/greeter-0.1.0-py3-none-any.whl
tar tzf dist/greeter-0.1.0.tar.gzIn the wheel listing, look for these entries:
greeter/data/greeting.txtgreeter/py.typedgreeter-0.1.0.dist-info/licenses/LICENSE
In the sdist listing, expect greeter-0.1.0/LICENSE and greeter-0.1.0/src/greeter/data/greeting.txt. Then repeat the runtime check in a clean environment, as covered in the chapter on testing installs:
python -m venv /tmp/verify-data
/tmp/verify-data/bin/pip install dist/greeter-0.1.0-py3-none-any.whl
/tmp/verify-data/bin/python -c "from greeter.templates import render; print(render('Ada'))"Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `FileNotFoundError` after `pip install` but not in your checkout | The data file is not matched by `package-data` and the editable install hid the problem | Add a pattern and rebuild the wheel |
| File appears in the sdist but not the wheel | It is listed in `MANIFEST.in` but sits outside any package directory | Move it inside `src/greeter/` and add a `package-data` pattern |
| Data file is missing from nested folders | The pattern `data/*.txt` does not recurse | Add a pattern for each level, such as `data/**/*.txt` |
| No license in `dist-info/licenses/` | Old setuptools or a wrong `license-files` pattern | Require `setuptools>=77` and check the path |
| Build error about `license` | A table-style `license` mixed with the new string form | Use one form, preferably the SPDX string |
Exercise
You add src/greeter/data/greeting.txt, run pip install -e ., and load_template() works. After python -m build and installing the wheel into a new virtual environment, it raises FileNotFoundError. What is the most likely cause, and how do you confirm and fix it?
Show answer
The file was never included in the wheel, and the editable install worked because it reads directly from your source tree. Confirm with python -m zipfile -l dist/greeter-0.1.0-py3-none-any.whl and check whether greeter/data/greeting.txt is listed. Fix it by adding [tool.setuptools.package-data] with greeter = ["data/*.txt"] to pyproject.toml, rebuilding, and reinstalling the new wheel in a clean environment.
Exercise
Where does the LICENSE file end up in the sdist and in the wheel when license-files = ["LICENSE"] is set, and which minimum setuptools version does this form need?
Show answer
In the sdist it is at the archive's top level, inside greeter-0.1.0/. In the wheel it is at greeter-0.1.0.dist-info/licenses/LICENSE. The PEP 639 form needs setuptools>=77.