Appendices
Appendix A — Traceback decoder: the 15 exceptions you will actually see
| Exception (last line of the traceback) | Usual cause | Fix |
|---|---|---|
NameError: name 'x' is not defined |
typo, or used before assignment, or missing import | check spelling and imports; run ruff check |
AttributeError: 'NoneType' object has no attribute 'x' |
a function returned None (often list.sort(), a failed re.match, or a missing return) |
check the value before use; type hints with X | None catch it (Ch 18.3) |
TypeError: unsupported operand type(s) for +: 'int' and 'str' |
mixing types; input from files/env is always str |
convert explicitly (int(...)) at the boundary |
TypeError: f() missing 1 required positional argument / takes 2 positional arguments but 3 were given |
wrong call signature; forgot self in a method |
read the signature; keyword arguments make calls explicit |
TypeError: 'NoneType' object is not subscriptable / not iterable |
indexing or looping over None |
as AttributeError above |
KeyError: 'x' |
missing dict key | d.get(k, default), defaultdict, or validate input with pydantic |
IndexError: list index out of range |
off-by-one, empty list | check if xs:; iterate instead of indexing |
ValueError: invalid literal for int() with base 10 |
parsing bad input | validate and handle at the boundary (Ch 7) |
UnboundLocalError: cannot access local variable |
assignment later in the function makes the name local (Ch 5.3) | rename, return a value, or nonlocal/global |
ModuleNotFoundError: No module named 'x' |
package installed into a different interpreter/venv; or a local file shadows it | uv run, python -m pip; check sys.executable and sys.path (Ch 0.1, 9.1) |
ImportError: cannot import name 'X' from partially initialized module |
circular import | move shared code to a third module (Ch 9.5) |
UnicodeDecodeError: 'utf-8' codec can't decode byte |
the file is not UTF-8, or is binary | pass the right encoding=, or errors="replace", or open in "rb" (Ch 3.4) |
RecursionError: maximum recursion depth exceeded |
unbounded recursion, often a property that calls itself | fix the base case; use an explicit stack (Ch 25.2) |
RuntimeError: dictionary changed size during iteration |
adding/removing keys inside for k in d |
iterate over list(d), or build a new dict |
ssl.SSLCertVerificationError: certificate verify failed |
missing CA bundle (macOS), corporate TLS interception, or a genuinely bad certificate | install certificates / truststore / REQUESTS_CA_BUNDLE; never verify=False (Ch 45.1) |
Two more you will meet in data work: RuntimeError: CUDA out of memory
(reduce batch size, use bfloat16, gradient accumulation; Ch 58.2) and
RuntimeError: mat1 and mat2 shapes cannot be multiplied (32x10 and 20x5)
(print .shape at every step; Ch 53.3).