Understanding Python messages
The following are the meanings of some of the classic python messages. Use CTLT + F in your browser to search for terms.
Note that if Python is reporting an issue with a line and you can't see anything wrong with it after close reading, this issue is probably
missing enclosing punctuation ([];();{};"";'';:
)in one of the lines above it.
python: can't open file 'misspelt_filenam.py': [Errno 2] No such file or directory
Filename misspelt.
Traceback (most recent call last):
File "misspelt_variable.py", line 3, in <module>
print(aa)
NameError: name 'aa' is not defined
A variable is not recognised on use (aa
). In this case it might be a misspelling. Note, however, that
misspelling on assignment (see variable A
in the file) will fail quietly by
creating a new variable. You see the same message when a variable isn't available within a block because of scoping problems.
Traceback (most recent call last):
File "misspelt_function.py", line 5, in <module>
f2()
NameError: name 'f2' is not defined
A function is not recognised. In this case, in the main script file. Again, this is often due to misspelling.
Traceback (most recent call last):
File "misspelt_module.py", line 1, in <module>
import numpty
ImportError: No module named 'numpty'
Misspelt module (in this case it should be numpy
. You also see this message if a module isn't installed, in which case,
try pypi/pip.
Traceback (most recent call last):
File "index_out_of_range.py", line 3, in <module>
print(nums[5])
IndexError: list index out of range
The index used to reference a value in a list or tuple is too big for the container, which contains fewer elements. This is a problem both with
assignment and use.
Traceback (most recent call last):
File "immutable.py", line 3, in <module>
nums[0] = 4
TypeError: 'tuple' object does not support item assignment
The code has tried to write to an immutable object, in this case a tuple.
File "unclosed_string.py", line 2
print(hello world")
^
SyntaxError: invalid syntax
This is a missing-punctuation issue.
Here the caret (^) is pointing at the position where Python finally fails to understand that
it is missing a double-quote from the start of the string. It is generally true that
Python can report syntax issues away from the actual problem, especially on the next line, for example, if
you're missing enclosing punctuation.
Traceback (most recent call last):
File "wrong_arguments.py", line 6, in <module>
f1(a)
TypeError: f1() missing 1 required positional argument: 'b'
This error usually means you haven't passed in enough arguments to a function, or you've passed in too many. The classic time this
occurs is when dealing with class functions, which are always invisibly passed the object when called from an object ("self
"): if you don't
include self
in the function parameters you'll get this message.
File "no_function_body.py", line 5
f1(1, 2)
^
IndentationError: expected an indented block
You get this message when there's no body to a function. If the function is mean to be empty, for example because
you're still building surrounding code, put pass
indented inside it, and the system will run. If it isn't
meant to be empty, chances are you've got a problem with dedents ending the function early.
Traceback (most recent call last):
File "cast_issue.py", line 2, in <module>
a = int([2,3,4])
TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'
An issue with trying to convert one data type to an incompatible type.
Traceback (most recent call last):
File "print_cast_issue.py", line 3, in <module>
print("1 + 1 = " + a)
TypeError: must be str, not int
Print won't cast values to strings when using in expressions, so in:
a = 2
print("1 + 1 = " + a)
The function will refused to print an int a
. The two options are to cast a
:
a = 2
print("1 + 1 = " + str(a))
or use the expanded print argument form:
a = 2
print("1 + 1 =", a) # Note adds space
File "wrong_indents.py", line 4
b = 2
^
IndentationError: unexpected indent
Incorrect indents and/or dedents. This is often seen when spaces and tabs are mixed in a file.
Traceback (most recent call last):
File "no_object.py", line 3, in <module>
b = a + 2
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
The object hasn't yet been set up. Either it hasn't been assigned by you, or, more usually, a function that didn't run has assigned the
value None
. This is a surprisingly common issue in Object Oriented Programming.