Sub topic 4
Describing your project: name, version, Python support, license, readme
This chapter assumes you can write a basic pyproject.toml with a [build-system] table, and that you know how to build a package with python -m build. Here we fill in the [project] table, which holds the metadata that PyPI and pip read.
The fields that identify your package
The [project] table is defined by the Python packaging standards, so every build backend reads it the same way. These fields come first:
[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",
]Only name is always required. version is also required unless you list it under dynamic. The other fields are optional, but a release without them looks unfinished on PyPI.
What each field does
| Field | Purpose | Common error |
|---|---|---|
| `name` | The name used with `pip install`. | The name is already taken on PyPI. |
| `version` | The release number, following PEP 440. | Reusing a version you already uploaded. |
| `description` | One-line summary shown in search results. | Putting multiple lines in it. |
| `readme` | File used as the long description on PyPI. | Wrong path, so the build fails. |
| `requires-python` | Python versions the package supports. | Leaving it out, so pip installs it on unsupported Python. |
| `license` | An SPDX license expression such as `MIT`. | Using the old `{text = "..."}` table form. |
| `license-files` | Files copied into the package metadata. | A glob that matches nothing. |
| `classifiers` | Trove tags that PyPI uses for filtering. | Inventing a classifier that PyPI does not recognize. |
Names, versions, and Python support
PyPI treats -, _, and . as equivalent in names, and it ignores case. So My_Package and my-package are the same project. Check that your name is free before you pick it.
The requires-python field matters more than it looks. pip reads it when resolving versions, and it skips releases that don't support the running interpreter. Set the lower bound to the oldest version you actually test.
Classifiers don't restrict anything. They only describe the package. A classifier saying Python 3.12 doesn't stop anyone from installing on 3.8, so use requires-python for enforcement.
Licence and readme
The license field takes an SPDX expression, a short standard identifier like MIT or Apache-2.0. Support for this form needs a recent backend, which is why the example requires setuptools>=77. With an older backend, the build rejects the string form.
license-files lists the actual license text to ship. Without it, your sdist and wheel may not carry the license at all.
For readme, the file extension picks the format: .md is Markdown and .rst is reStructuredText. PyPI renders it on your project page. If the content is malformed, twine check reports it before you upload.
Reading the version from one place
Keep the version number in pyproject.toml only. The package can read it back from the installed metadata:
# src/greeter/__init__.py
from importlib.metadata import version
__version__ = version("greeter")This works only after the package is installed, for example with pip install -e .. Importing from an uninstalled source tree raises PackageNotFoundError.
Exercise
A user on Python 3.8 runs pip install greeter, and your package sets requires-python = ">=3.9". What does pip do, and why can't a classifier achieve the same thing?
Show answer
pip skips releases whose requires-python excludes 3.8 and installs an older compatible release if one exists, or reports that no matching version was found. Classifiers are descriptive tags for PyPI filtering. pip does not use them to block installs.
Exercise
You get PackageNotFoundError when you import greeter from a fresh clone. What is the likely cause, and what fixes it?
Show answer
The package was never installed, so importlib.metadata has no metadata to read. Run pip install -e . in a virtual environment and import again.