After conquering async, it is time to look at the standard library and packaging workflow Python ships by default. The terms “standard library” and “packaging” may sound grand, but think of them as “pick from built-in modules” and “zip a folder for others.” This lesson lowers the barrier by moving through essential tools → pyproject review → build flow.
Key terms
- Standard library: the modules Python provides out of the box, no extra install needed
- wheel: a
.whlfile that ships pre-built artifacts - sdist: a
.tar.gzarchive that contains source code and builds during install - uv build: command that produces both wheel and sdist for a pyproject-based project
Core ideas
Study notes
- Time required: 80–90 minutes (excluding optional sections)
- Prerequisites: experience with
pyprojectanduv, testing and logging setup, CLI work- Goal: apply two or three standard modules to your project and run
uv buildto prepare for distribution
- The standard library ships with Python itself.
pyproject.tomlstores project metadata and dependencies.uv buildoutputs both wheel and sdist artifacts.- A wheel is pre-built; an sdist builds later.
- Clear the Core parts first; mark optional sections for future reference.
Code examples
Standard modules worth knowing (Core)
pathlib: treat paths as objects.
from pathlib import Path
project_root = Path(__file__).parent
for path in project_root.glob("*.md"):
print(path.stem)
dataclasses: reduce repetitive initializers.
from dataclasses import dataclass
@dataclass
class Report:
title: str
data: list[dict]
datetime: handle time zones and durations. Combine withzoneinfofor official time-zone data.collections: use structures likeCounter,defaultdict, anddeque.logging: produce consistent log output.
Review pyproject.toml (Core)
We briefly touched on this file when building a Typer CLI; now we will look at it properly.
[project]
name = "mealbot"
version = "0.1.0"
description = "School meal notifier CLI"
readme = "README.md"
requires-python = ">=3.12"
dependencies = [
"typer",
"httpx",
]
[project.optional-dependencies]
dev = ["mypy", "pytest"]
[project.urls]
homepage = "https://example.com"
- The
projecttable follows PEP 621 metadata rules. optional-dependencieslets you install groups likepip install mealbot[dev].- If the full form feels heavy, start with
name,version, anddependencies, then expand gradually.
Build and release flow (Core → Plus)
Modern frontends such as uv, hatch, and pdm differ in UX, but the pipeline is the same.
uv build # create wheel and sdist
uv publish --token $PYPI_TOKEN
- The wheel is a binary distribution; the sdist contains source.
- For private releases, point to an internal index or a Git URL.
- If “build” sounds intimidating, remember you are simply generating two archives in
dist/.
direction: right
code: "src/
tests pass"
pyproject: "pyproject.toml
metadata"
build: "uv build
wheel/sdist"
dist: "dist/
.whl .tar.gz"
publish: "uv publish
PyPI/TestPyPI"
code -> pyproject: "record version/deps"
pyproject -> build: "feed build settings"
build -> dist: "produce artifacts"
dist -> publish: "upload"
publish -> code: "feedback for next release"
Visualizing the stages makes it easy to turn them into a checklist: organize code → fill out metadata → build → publish.
Ship a CLI using only the stdlib (Optional)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("school_code")
args = parser.parse_args()
print(args.school_code)
if __name__ == "__main__":
main()
Even without Typer, the trio of argparse, logging, and pathlib brings you a solid CLI. If you already prefer Typer, skim this as a reminder. You can also bundle the script with zipapp if needed.
Packaging checklist (Optional)
- Run
python -m buildoruv buildto create packages. - Validate metadata with
uvx twine check dist/*. - Upload to TestPyPI first and perform an install test.
- Confirm README, examples, and license are included.
Why it matters
- Leaning on the standard library cuts dependencies and simplifies maintenance.
pyproject.tomlis the canonical home for package metadata.- Automating build and release steps via scripts or CI keeps quality consistent.
Practice
- Follow along: Build a small CLI using at least two modules from
pathlib,dataclasses, orargparse, then run it withuv run. - Extend: Add an
optional-dependenciesgroup topyproject.tomland runuv build, inspecting the new files indist/. - Debug: Trigger a warning during
uvx twine check, then fix it by filling in metadata such as description or README link. - Definition of done: You have examples that use standard modules plus a build log, and optional sections are captured as future exercises.
Wrap-up
The final lesson ties every concept together in a mini project so you can experience how these tools line up in practice.
💬 댓글
이 글에 대한 의견을 남겨주세요