`NameError: name 'log' is not defined` only when an error path runs in production.
BeginnerTroubleshootingRuntime, file, subprocess & encoding errors
Answer
Likely causes → A typo or missing import in a rarely executed branch (often an except block), which Python only detects when that line runs.
Diagnose → Run ruff check (F821 Undefined name) and mypy; both find this statically.
Fix → Define/import the name; add a test that exercises the error path.
Prevent → Linting in CI catches this class of bug in seconds; test failure paths, not just the happy path.
More on Runtime, file, subprocess & encoding errors
`KeyError: 'Tags'` while processing `describe_instances` output.Beginner`TypeError: can't compare offset-naive and offset-aware datetimes` in a snapshot-age check.Beginner`UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte` reading a log file.Intermediate`UnicodeEncodeError: 'ascii' codec can't encode character '✓'` when printing in a container.Intermediate`PermissionError: [Errno 13] Permission denied: '/var/log/app/report.json'`Intermediate`OSError: [Errno 24] Too many open files` in a long-running Python service.Intermediate