Sub topic 1
What `pyproject.toml` Describes
Prerequisites
You should know basic Python, imports, packages, and how to use a terminal. You should also understand that a virtual environment keeps project packages separate from system packages.
One file for project configuration
pyproject.toml is a configuration file that describes a Python project. It uses TOML, a text format made of named tables and key-value settings.
The [project] table contains standard metadata:
nameandversionidentify the project.requires-pythonstates which Python versions are supported.dependencieslists packages needed at runtime.[project.scripts]can expose a Python function as a shell command.
Other tables have different responsibilities:
[dependency-groups]stores tools needed for development, such as test runners.[build-system]selects the backend that builds wheels and source archives.[tool.uv]contains uv-specific settings, including dependency sources and workspace options.
[project]
name = "weather-cli"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = ["httpx>=0.27"]
[dependency-groups]
dev = ["pytest>=8"]
[tool.uv]
package = trueWhere uv fits
uv is a Python tool manager. It can install Python versions, create and update project environments, resolve dependencies, and run commands. It also runs standalone tools without permanently adding them to a project.
The configuration is declarative: you state what the project needs, and uv calculates an environment that satisfies those requirements.
Create a project and choose Python
Start with a packaged project:
uv init --package weather-cli
cd weather-cli
uv python pin 3.12uv init creates the initial files, including pyproject.toml. uv python pin records the preferred interpreter version, usually in .python-version. The requires-python field remains the compatibility rule for the project. These settings can differ: a project may run on Python 3.12 while supporting 3.12 and newer versions.
Add, remove, and update dependencies
Use uv commands rather than editing dependency lists by hand:
uv add httpx
uv add --dev pytest
uv remove httpx
uv lock --upgrade-package pytestuv add updates pyproject.toml and resolves the result into uv.lock. Development dependencies stay separate from packages required by users of the project. uv remove removes a declared package. The upgrade command asks uv to select a newer compatible version.
Exercise
Where should a test runner such as pytest be declared?
Show answer
In the development dependency group, because the application does not need it at runtime.