Sub topic 4
Describing your project: name, version, Python support, license, readme
This chapter covers the [project] table in pyproject.toml. It assumes you can already write and import Python packages, use a virtual environment and pip, and run basic Git and command-line commands. It also assumes you have a src layout and a [build-system] table, which earlier chapters cover. Here the focus is on the fields that PyPI and pip read to describe your library.
What the [project] table does
The [project] table holds your project's core metadata. A build backend reads it and writes it into the built artifacts, in a file called METADATA. After that, pip uses it to decide whether your package can be installed on a given Python. PyPI uses it to render your project page.
The table is defined by a standard (PEP 621), so every compliant backend reads the same keys. You are not tied to one tool's configuration format.
[build-system]
requires = ["setuptools>=77"]
build-backend = "setuptools.build_meta"
[project]
name = "greeter"
version = "0.1.0"
description = "A tiny library that builds greeting messages."
readme = "README.md"
requires-python = ">=3.9"
license = "MIT"
license-files = ["LICENSE"]
classifiers = [
"Programming Language :: Python :: 3",
]
dependencies = []Only name is always required. version is required unless you declare it dynamic, which is covered below. Every other field is optional, but you should fill in the ones in the example for any library you plan to publish.
Name
The name field is the name people type after pip install. It is also the name PyPI uses to identify your project.
name = "greeter"Rules and details to keep in mind:
- A name may contain ASCII letters, digits,
.,-and_. It must start and end with a letter or digit. - Comparison ignores case and treats runs of
.,-and_as the same character. SoMy_Library,my-libraryandmy.libraryall refer to one project. This is called normalization. - The distribution name and the import name are separate things. The distribution name is what you install (
pip install python-dateutil). The import name is the package directory undersrc/(import dateutil). They often match, but nothing requires it. Hyphens cannot appear in import names, so a project namedmy-libraryusually has the import packagemy_library. - Names on PyPI are first come, first served. Check that your name is free before you build anything. Visit
https://pypi.org/project/<name>/and look for a 404. A free name can still be rejected if it is too similar to an existing one, so a TestPyPI rehearsal (a later chapter) is worth doing.
A generic name such as utils is almost certainly taken. A distinctive name also makes it less likely that a user confuses your library with a different one.
Version
The version field is a string that identifies one release.
version = "0.1.0"PyPI rejects an upload if a file with the same name and version already exists, even if you deleted the earlier release. So each upload needs a new version number. A later chapter covers how to choose numbers under PEP 440 and semantic versioning. For now, keep these points in mind:
- The version string must be valid under PEP 440.
0.1.0,1.0.0rc1and2.3.4.post1are valid.v1.0and1.0-finalare not. - Keep the number in exactly one place. If you write it in
pyproject.tomland again in__init__.py, the two will drift apart sooner or later.
The chapter's example package reads the version from the installed metadata instead of repeating it:
# src/greeter/__init__.py
from importlib.metadata import version
__version__ = version("greeter")
def greet(name: str) -> str:
return f"Hello, {name}!"This works only after the package is installed, because importlib.metadata reads the metadata of an installed distribution. During development, install your project in editable mode (python -m pip install -e .) so the metadata exists. If you import the package from a plain source tree without installing it, version("greeter") raises PackageNotFoundError.
Dynamic fields
If you would rather keep the version in a Python file or derive it from Git tags, list it under dynamic and leave out the static version key.
[project]
name = "greeter"
dynamic = ["version"]The backend must then know how to compute the field. With setuptools, you would add a [tool.setuptools.dynamic] table. Tag-based tools such as setuptools-scm supply the value from Git. A field cannot appear both as a static value and in dynamic, and a backend that does not support dynamic computation of a field will fail the build. For a small library, the static version is the simplest choice.
Description and readme
description is a one-line summary. PyPI shows it in search results and under the project name, so keep it short and concrete.
readme points to a file whose content becomes the long description on your PyPI page.
description = "A tiny library that builds greeting messages."
readme = "README.md"The backend infers the content type from the file extension: .md is Markdown, .rst is reStructuredText, and .txt is plain text. If you need to be explicit, use a table.
readme = { file = "README.md", content-type = "text/markdown" }Common readme mistakes:
- The path is relative to
pyproject.toml. A typo gives a build error, which is the good case. - Relative links and images in the README (
) break on PyPI, because PyPI does not have your repository files. Use absolute URLs. - Markup errors can make PyPI refuse to render the page.
twine check, which a later chapter covers, catches many of these before you upload.
Python support
Two fields describe which Pythons your library supports, and they do different jobs.
requires-python is a rule that pip enforces. When a user runs pip install greeter on Python 3.8, pip looks for the newest release whose requires-python allows 3.8, and installs that one instead. If none allow it, the install fails.
requires-python = ">=3.9"classifiers are labels for humans and search filters. PyPI does not enforce them. They come from a fixed list of strings (the "trove classifiers") that PyPI validates.
classifiers = [
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.12",
]An unknown classifier makes PyPI reject the upload, so copy the strings from the official list rather than inventing them.
| Field | Who reads it | Enforced by pip? | Purpose |
|---|---|---|---|
| `requires-python` | pip and other installers | Yes | Blocks installs on unsupported Pythons |
| `classifiers` | People and PyPI search | No | Describes supported versions, status and audience |
Keep the two consistent. Listing a classifier for Python 3.8 while requires-python says >=3.9 misleads readers.
Avoid an upper bound such as <4, unless you know of a real incompatibility. Upper bounds on requires-python cause the installer's resolver to reject your package on a new Python release, and they are hard to correct after the fact, since old releases keep the old bound. Only test the Python versions you list. A version you never run in CI is a version you do not know works.
License
The license field uses an SPDX license expression, a standard short code. MIT, Apache-2.0 and BSD-3-Clause are examples. Expressions can combine licenses, as in MIT OR Apache-2.0.
license = "MIT"
license-files = ["LICENSE"]license-files lists glob patterns for the files that hold the full license text. The backend copies them into the built artifacts, so a user who installs your wheel receives the license along with the code.
An SPDX expression is not a license file. Always include the LICENSE file as well. Without any license, others have no legal right to use your code, even if it is public.
Other useful fields
You can add more metadata as your project matures. None of it is needed for a first release.
authorsandmaintainersare lists of tables withnameandemail.keywordsis a list of search terms.[project.urls]links your homepage, documentation, source repository and issue tracker. PyPI shows these in the sidebar.
authors = [{ name = "Ada Example", email = "[email protected]" }]
keywords = ["greeting", "example"]
[project.urls]
Homepage = "https://example.com/greeter"
Source = "https://github.com/example/greeter"
Issues = "https://github.com/example/greeter/issues"Place plain keys such as authors and keywords before any [project.urls] header. In TOML, a key written after a table header belongs to that table, so an authors line placed under [project.urls] would silently land in the wrong table.
Check the result
After a build, you can read the metadata that the backend produced. This confirms that your fields were interpreted as you intended. Wheels are zip files, so unzip -p prints one file from inside.
python -m pip install build
python -m build --wheel
unzip -p dist/greeter-0.1.0-py3-none-any.whl 'greeter-0.1.0.dist-info/METADATA' | head -20Look for Name, Version, Requires-Python and License-Expression. A later chapter covers inspecting artifacts in more detail.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `Invalid version` during build | The version is not PEP 440, for example `v1.0` | Use `1.0` |
| Upload rejected: file already exists | The version was uploaded before | Increase the version and rebuild |
| Upload rejected: invalid classifier | A classifier string is not in the official list | Copy the exact string from the list |
| `PackageNotFoundError` on import | The package was not installed, so no metadata exists | Run `python -m pip install -e .` |
| Readme renders as plain text on PyPI | Wrong or missing content type | Set `content-type` explicitly |
| Build error about `license` | Backend is too old for SPDX expressions | Raise the `setuptools` lower bound |
Exercise
Your library requires Python 3.10 or newer. A user on Python 3.8 runs pip install yourlib. Which field decides what pip does, and why is adding the classifier Programming Language :: Python :: 3.10 not enough to protect that user?
Show answer
requires-python = ">=3.10" decides it. pip reads that field and refuses to install a release whose requirement the running Python does not meet (it falls back to an older compatible release if one exists). Classifiers are only labels for people and search filters, and pip does not enforce them, so a classifier cannot block an install.
Exercise
You release version 1.2.0, notice a typo in the README, delete the release on PyPI, fix the typo and try to upload 1.2.0 again. What happens, and what should you do instead?
Show answer
PyPI rejects the upload, because a filename with that version was already used and cannot be reused even after deletion. Bump the version, for example to 1.2.1 (or 1.2.0.post1), rebuild, and upload the new files.