Skip to main content

Introduction to Python

Learn Python for DevOps with real output: Python 3.14 and uv, files and YAML, subprocess, HTTP retries, logging, classes, pytest and a packaged CLI.

Python is the language DevOps engineers reach for when a shell script stops being enough: when you need to parse JSON or YAML, call an HTTP API with retries, handle errors properly, or test what you wrote. Cloud SDKs such as boto3, tools such as Ansible, and countless internal scripts and CLIs are written in it.

This guide teaches Python from the first line, through the lens of operations work. Every example is a small piece of something you would really write: reading settings from environment variables, waiting for a port to open, rolling out in batches, finding drift between two inventories, parsing gzipped logs, calling an API with timeouts and retries, running commands without a shell. Along the way the pieces grow into svcheck, a tested and packaged command-line tool that runs service checks from a TOML file, built in Part 10.

What You'll Learn

  1. Install Python 3.14 with uv: which version to use in October 2026, uv and the system Python, the PEP 668 "externally managed" error, virtual environments, the REPL and your first script.
  2. Variables and Data Types: numbers and their traps, strings and f-strings, bytes versus str, truthiness, and settings from environment variables.
  3. Control Flow: if, match, for loops over hosts and batches, and while loops with deadlines, backoff and jitter.
  4. Functions: parameters, type hints checked by mypy, the mutable default trap, generators, recursion and a retry decorator.
  5. Data Structures: lists, dictionaries, sets, comprehensions, Counter and defaultdict, deep copies, and a drift report.
  6. Modules and Packages: imports and packages, the standard library, virtual environments, pip and uv, lock files, a supply-chain cooldown and PEP 723 scripts.
  7. File Operations: pathlib, encodings, gzipped logs, JSON, YAML and TOML (and their traps), atomic writes, permissions and safe tar extraction.
  8. Error Handling: tracebacks, exceptions, logging, exit codes and SIGTERM, subprocess without injection, and HTTP calls with timeouts and retries.
  9. Object-Oriented Programming: classes, dataclasses, enums, properties, special methods, Protocols and an API client class.
  10. Building a Python Project: svcheck with a src layout, pyproject.toml, argparse, threads, pytest, Ruff, mypy --strict, Python 3.12 to 3.15, boto3 and CI.

Python for DevOps at a Glance

  • Never install into the system Python. Use a virtual environment per project and uv tool install for tools (Part 1, Part 6).
  • Lock your dependencies with uv.lock (or hashed requirements) and update them on purpose (Part 6).
  • Convert and validate everything from outside: environment variables, files and API responses are strings or untyped data until you check them (Part 2, Part 7).
  • Every wait and every network call gets a timeout, and retries get backoff and jitter (Part 3, Part 8).
  • Pass encoding="utf-8", write important files atomically, and use yaml.safe_load() (Part 7).
  • Run commands with a list of arguments and check=True, never a shell string built from input (Part 8).
  • Log to standard error, exit with meaningful codes, and handle SIGTERM (Part 8).
  • Type hints, Ruff, mypy and pytest in CI, on every Python version you claim to support (Part 10).

Which Versions This Guide Uses

The guide was updated on 24 October 2026. Python 3.15.0 was released on 9 October 2026 and 3.15.1 on 21 October; the guide uses Python 3.14, the mature current release (see Part 1 for why), and the tool built in Part 10 is also tested on 3.12 and 3.15. The terminal output comes from real runs as a normal user (dev) on a machine called devbox:

  • CPython 3.14.9 installed with uv 0.13.5 on Debian 12 (arm64), for almost everything.
  • Ubuntu 26.04.1 LTS with its own Python 3.14.4 and pip 25.1.1, for the "externally managed environment" and python3-venv errors in Part 1.
  • CPython 3.12.15 and 3.15.1, for the Part 10 tests on other versions.
  • httpx 0.28.1, PyYAML 6.0.3, pytest 9.1.1, Ruff 0.17.2, mypy 2.4.0, boto3 1.43.118 and actionlint 1.7.12.

What is not a real run: the macOS and Windows tabs, the requests tab in Part 8, the systemd, cron and container examples in Part 10, the GitHub Actions workflow (checked with actionlint, not run on GitHub), and any call to a real AWS account (the AWS runs in Part 10 use no credentials and botocore's Stubber). Timings come from a small ARM board, so yours will be faster.

If you learned Python from an older tutorial, these are the changes that matter most:

Old habit Now Why
sudo pip install x A virtual environment, or uv tool install x PEP 668 blocks it, and it can break system tools
setup.py and a bare requirements.txt pyproject.toml and a lock file (uv.lock) Standard metadata; exact, hashed, reproducible installs
"%s" % x and "{}".format(x) f-strings (logging keeps % style) Shorter and easier to read
os.path.join() and friends pathlib.Path One object for paths, listing, reading and writing
open(path) open(path, encoding="utf-8") The default depended on the locale until 3.15
datetime.utcnow() datetime.now(UTC) Deprecated since 3.12; naive datetimes get mixed up
yaml.load(f) yaml.safe_load(f) Can run code; an error without a Loader since PyYAML 6
requests.get(url) httpx.get(url), or requests with timeout= requests has no default timeout
os.system(cmd), shell=True with input subprocess.run([...], check=True, timeout=...) Injection, ignored failures, hangs
tar.extractall(path) tar.extractall(path, filter="data") Path traversal; data is the default since 3.14
asyncio.get_event_loop().run_until_complete() asyncio.run(main()) get_event_loop() raises without a loop since 3.14
import telnetlib, cgi, distutils Maintained libraries, setuptools Removed in Python 3.12 and 3.13

Prerequisites

  • A computer with Linux, macOS or Windows, and a terminal. On Windows, follow the guide in WSL 2, because the examples use Linux commands. Part 1 installs everything else.
  • Basic command-line use. If cd, pipes and environment variables are new to you, read the Linux command line basics first; the Bash guide is a good companion, since Python scripts usually run from the shell.
  • No previous programming experience is needed, though it helps.

Practice Hands-On

Parts 1 to 9 work in one directory, ~/pyops, with one virtual environment, created in Part 1; Part 10 builds a separate project, ~/svcheck. Each example is a complete file you can save and run, and the output you see is what it printed. Part 8 gives you a small local test API, so the HTTP examples work without any account or internet service.

Keep Learning

Start with Part 1: Install Python 3.14 with uv, or jump to error handling, subprocess and HTTP retries if that is what you need today.

Last updated:

Found an issue?