Home·Error messages

python · Python

The message

KeyError: 'name'

What it means

The key is not in the dict, and the string in quotes is exactly the key it looked for — start there, because it is usually a typo, a case difference, or an API response whose shape is not what you assumed. If the value is genuinely optional, d.get("name") returns None instead and d.get("name", 0) supplies a default; but using .get on a value that must be there trades the error for a None that travels into your code and fails much further away.

The fix

There is no one-line command for this. The explanation says what to look at instead.

Printed by
python
Python
18

A Python traceback splits the answer in two — the last line says what went wrong, the frames above it say where — so reading only the last line gives you the name and loses the place, and for the value-is-missing errors such as NoneType and KeyError the cause almost always sits in a frame above the one that crashed.

Reading an error message

  • Read from the first line down. The lower you go the more it is about the tool’s internals; the cause is usually at the top.
  • If there is a file and a line number, start there — not the top stack frame, but the topmost line that names a file you wrote.
  • Search the message verbatim, but strip your own paths and variable names first; those are what stop the search from matching.
  • The same condition is worded differently across tool versions. If results look wrong, add the version number to the query.
  • Before pasting a fix, check what it throws away. Some of these cannot be undone.

Common questions

Q. What does “KeyError: 'name'” mean?

The key is not in the dict, and the string in quotes is exactly the key it looked for — start there, because it is usually a typo, a case difference, or an API response whose shape is not what you assumed. If the value is genuinely optional, d.get("name") returns None instead and d.get("name", 0) supplies a default; but using .get on a value that must be there trades the error for a None that travels into your code and fails much further away.

Q. How do I fix it?

There is no one-line command. The explanation above says what to look at instead.

Q. Which tool prints this?

python. It sits under Python, and the message runs to 2 words.

Errors nearby