Python Errors and How to Fix Them
When a program stops with an error, the last line of its message names the error. Find it here: what it means, what to check, and the usual causes, each shown broken and fixed.
- AttributeError The value has no method or attribute by that name. The message names the value's type and what was asked for.
- FileNotFoundError Python couldn't find the file to open. Its name is looked up next to your program, so the file has to be there with exactly that name.
- IndentationError The spaces at the start of a line don't fit the block it belongs to. Python uses indentation to know which lines belong together.
- IndexError A position was asked for that the list or string doesn't have.
- KeyError The dictionary has no such key. Python names the key it looked for after KeyError.
- ModuleNotFoundError Python couldn't find the module to import: its name is spelled differently, the file isn't in the project, or the package isn't installed here.
- NameError Python reached a name it has no value for: a variable, function or module it hasn't seen created yet.
- RecursionError A function kept calling itself and never stopped. Python stops it after about 1,000 calls.
- SyntaxError Python couldn't read your code, so none of it ran. The mistake is on the line the message points to, or just above it.
- TypeError A value was used in a way its type doesn't allow, like adding text to a number or calling something that isn't a function.
- UnboundLocalError A function used a variable before giving it a value. Assigning to a name anywhere in a function makes it that function's own variable.
- ValueError The value has the right type but one Python can't use, like the text "12.50" given to int().
- ZeroDivisionError A number was divided by zero, which has no answer. The zero is usually a count or a length that came out empty.