Common Python errors
what is written and what it means
What it is
Real Python 3.13 messages — all obtained by running, not from memory. Read them from the bottom up: the last line says what happened, the lines above say where (article 2.12).
Names and types
NameError: name 'cuty' is not defined
The name doesn't exist. Usually a typo, or a variable declared further down
the file than it is used.
AttributeError: 'str' object has no attribute 'push'
The object has no such method. A frequent sign that the AI mixed up
languages: push belongs to arrays in JavaScript; Python lists have
append.
TypeError: can only concatenate str (not "int") to str
A string was added to a number. Almost always the value came from an
environment variable or from input and stayed a string (article 13.4).
TypeError: 'NoneType' object is not subscriptable
A key or index was used on None. Which means a function above returned
None instead of data — and that wasn't checked.
TypeError: f() missing 1 required positional argument: 'b'
Called with too few arguments. Often after a signature was edited and not
every call site was updated.
UnboundLocalError: cannot access local variable 'n' where it is not associated with a value
Inside a function, a value was assigned to a variable declared outside.
Python treats it as local. The cure is passing it as an argument, not
global.
Data
KeyError: 'temp'
The dictionary has no such key. The commonest error when parsing API
responses: the key is there when all is well and disappears when the server
returns an error.
IndexError: list index out of range
An index past the end of the list. Check the list isn't empty — and
remember the last element is [-1], not [len(xs)].
ValueError: invalid literal for int() with base 10: '-2.5'
The string doesn't turn into a number. int() doesn't understand decimal
notation — you want float() and then rounding.
ZeroDivisionError: division by zero
Division by zero. Usually the denominator is the length of an empty list or
a counter that hasn't had time to grow.
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Something that isn't JSON was parsed as JSON: an empty string, an HTML
error page, a truncated file. What to print is the raw response text, not
the parsed data.
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd8 in position 96
A binary file was opened as text, or text in another encoding. For binary,
the "rb" mode; for text, an explicit encoding=.
Files and modules
FileNotFoundError: [Errno 2] No such file or directory: 'cities.json'
The file isn't there where the program was started from. A relative
path is counted from the working directory, not from the file with the code
(article 12.1).
ModuleNotFoundError: No module named 'rekvests'
The library isn't installed, or the name is misspelled. If the name looks
strange, it may not exist at all (article 4.8).
ImportError: cannot import name 'x' from partially initialized module 'y' (most likely due to a circular import)
Two modules import each other. That isn't a Python error but a signal of
mixed-up layers (article 13.1).
Syntax and execution
IndentationError: expected an indented block after function definition on line 1
The indentation was lost. Usually when pasting code in pieces.
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
One equals sign instead of two in a condition. Python says so right in the
error text.
RecursionError: maximum recursion depth exceeded
A function calls itself with no stopping condition. The traceback shows
[Previous line repeated 996 more times].
sqlite3.IntegrityError: UNIQUE constraint failed: cities.name
A table rule was broken: a record with that value already exists. That
isn't a breakage but a guard doing its job — catch it and return a clear
response (article 14.2).