[Python Series 19] Using Python's Standard Library and Packaging Your Code

한국어 버전

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

  1. Standard library: the modules Python provides out of the box, no extra install needed
  2. wheel: a .whl file that ships pre-built artifacts
  3. sdist: a .tar.gz archive that contains source code and builds during install
  4. 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 pyproject and uv, testing and logging setup, CLI work
  • Goal: apply two or three standard modules to your project and run uv build to prepare for distribution
  • The standard library ships with Python itself.
  • pyproject.toml stores project metadata and dependencies.
  • uv build outputs 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 with zoneinfo for official time-zone data.
  • collections: use structures like Counter, defaultdict, and deque.
  • 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 project table follows PEP 621 metadata rules.
  • optional-dependencies lets you install groups like pip install mealbot[dev].
  • If the full form feels heavy, start with name, version, and dependencies, 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)

  1. Run python -m build or uv build to create packages.
  2. Validate metadata with uvx twine check dist/*.
  3. Upload to TestPyPI first and perform an install test.
  4. Confirm README, examples, and license are included.

Why it matters

  • Leaning on the standard library cuts dependencies and simplifies maintenance.
  • pyproject.toml is 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, or argparse, then run it with uv run.
  • Extend: Add an optional-dependencies group to pyproject.toml and run uv build, inspecting the new files in dist/.
  • 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.

💬 댓글

이 글에 대한 의견을 남겨주세요