Python Errors Explained
The 40 error messages beginners meet most, each with the code that causes it and the fix.
Syntax and indentation
- IndentationError: expected an indented block
- IndentationError: unexpected indent
- IndentationError: unindent does not match any outer indentation level
- TabError: inconsistent use of tabs and spaces in indentation
- SyntaxError: invalid syntax
- SyntaxError: unterminated string literal
- SyntaxError: '(' was never closed
- SyntaxError: expected ':'
- SyntaxError: invalid character '“' (U+201C)
- SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
NameError and scope
TypeError
- TypeError: 'int' object is not subscriptable
- TypeError: 'NoneType' object is not subscriptable
- TypeError: can only concatenate str (not "int") to str
- TypeError: unsupported operand type(s) for +: 'int' and 'str'
- TypeError: 'str' object is not callable
- TypeError: 'list' object is not callable
- TypeError: 'int' object is not iterable
- TypeError: object of type 'int' has no len()
- TypeError: greet() missing 1 required positional argument: 'name'
- TypeError: add() takes 2 positional arguments but 3 were given
- TypeError: 'str' object does not support item assignment
- TypeError: list indices must be integers or slices, not str
- TypeError: unhashable type: 'list'
ValueError
IndexError and KeyError
AttributeError
Files, modules, maths and recursion
New to tracebacks? Start with how to read Python error messages.