Python Error Reference

A comprehensive collection of common Python exceptions, their causes, and how to fix them.

Fix AssertionError

assert 1 == 2...

Learn how to resolve the AssertionError in Python. Assertion failed. Check your assumption....

Fix AttributeError

[1, 2, 3].push(4)...

Learn how to resolve the AttributeError in Python. List object has no attribute 'push'. Did you mean ...

Fix AttributeError

None.some_method()...

Learn how to resolve the AttributeError in Python. 'NoneType' object has no attribute 'some_method'....

Fix BlockingIOError

os.write(fd, data)...

Learn how to resolve the BlockingIOError in Python. Resource temporarily unavailable (non-blocking I/O...

Fix ConnectionRefusedError

socket.connect(('localhost', 8...

Learn how to resolve the ConnectionRefusedError in Python. Connection refused. Check server status and port....

Fix DeprecationWarning

import imp...

Learn how to resolve the DeprecationWarning in Python. imp module is deprecated. Use importlib....

Fix EOFError

input('')...

Learn how to resolve the EOFError in Python. End of file reading input (e.g. user pressed Ctrl+...

Fix FileExistsError

os.makedirs('existing_dir')...

Learn how to resolve the FileExistsError in Python. File exists: 'existing_dir'. Handle existence or u...

Fix FileNotFoundError

open('missing.txt')...

Learn how to resolve the FileNotFoundError in Python. The file 'missing.txt' was not found. Check the pa...

Fix FloatingPointError

1.0 / 0.0...

Learn how to resolve the FloatingPointError in Python. Float division by zero (usually raises ZeroDivisio...

Fix ImportError

import unknown_module...

Learn how to resolve the ImportError in Python. No module named 'unknown_module'. Check installati...

Fix IndentationError

def my_func(): print('hello')...

Learn how to resolve the IndentationError in Python. The body of the function must be indented....

Fix IndentationError

expected an indented block...

Learn how to resolve the IndentationError in Python. Missing indentation after 'if' or 'def'....

Fix IndexError

my_list = [1, 2, 3]; print(my_...

Learn how to resolve the IndexError in Python. List index out of range. Check the list length....

Fix IsADirectoryError

open('/tmp', 'r')...

Learn how to resolve the IsADirectoryError in Python. Is a directory: '/tmp'. Cannot open directory as f...

Fix KeyboardInterrupt

while True: pass...

Learn how to resolve the KeyboardInterrupt in Python. User interrupted execution (Ctrl+C)....

Fix KeyError

my_dict = {'a': 1}; print(my_d...

Learn how to resolve the KeyError in Python. Key 'b' is not in the dictionary. Use .get() or ch...

Fix KeyError

os.environ['MISSING_ENV']...

Learn how to resolve the KeyError in Python. Environment variable not found....

Fix LookupError

codecs.lookup('unknown')...

Learn how to resolve the LookupError in Python. Unknown encoding....

Fix MemoryError

x = [1] * (10**10)...

Learn how to resolve the MemoryError in Python. Out of memory. Optimize data structures or algorit...

Fix ModuleNotFoundError

import non_existent...

Learn how to resolve the ModuleNotFoundError in Python. No module named 'non_existent'. Install it via pip...

Fix NameError

print(undefined_var)...

Learn how to resolve the NameError in Python. The variable 'undefined_var' is not defined. Ensur...

Fix NotADirectoryError

os.listdir('/etc/hosts')...

Learn how to resolve the NotADirectoryError in Python. Not a directory: '/etc/hosts'....

Fix NotImplementedError

def foo(): raise NotImplemente...

Learn how to resolve the NotImplementedError in Python. This method is not implemented yet....

Fix OSError

os.remove('non_existent')...

Learn how to resolve the OSError in Python. No such file or directory....

Fix OverflowError

import math; math.exp(1000)...

Learn how to resolve the OverflowError in Python. Math result too large. Handle large numbers or lim...

Fix PermissionError

open('/root/secret', 'r')...

Learn how to resolve the PermissionError in Python. Permission denied. Check file permissions....

Fix RecursionError

def foo(): foo(); foo()...

Learn how to resolve the RecursionError in Python. Maximum recursion depth exceeded. Check base case ...

Fix ReferenceError

import weakref; r = weakref.re...

Learn how to resolve the ReferenceError in Python. Weakly-referenced object no longer exists....

Fix RuntimeError

recursion limit...

Learn how to resolve the RuntimeError in Python. Maximum recursion depth exceeded (can be RuntimeEr...

Fix StopIteration

it = iter([1]); next(it); next...

Learn how to resolve the StopIteration in Python. Iterator has no further items. Catch StopIteration...

Fix SyntaxError

def function(a, b):...

Learn how to resolve the SyntaxError in Python. Missing body or 'pass'....

Fix SyntaxError

if x = 5:...

Learn how to resolve the SyntaxError in Python. Invalid syntax. Use == for comparison....

Fix SyntaxError

print 'Hello World'...

Learn how to resolve the SyntaxError in Python. In Python 3, print is a function. Use print('Hello...

Fix SyntaxError

return...

Learn how to resolve the SyntaxError in Python. Return outside function....

Fix SystemError

sys.exit()...

Learn how to resolve the SystemError in Python. SystemError can happen on internal errors, but sys...

Fix TabError

def foo(): print('x') pri...

Learn how to resolve the TabError in Python. Inconsistent use of tabs and spaces in indentation...

Fix TimeoutError

socket.timeout...

Learn how to resolve the TimeoutError in Python. The operation timed out....

Fix TypeError

'a' - 'b'...

Learn how to resolve the TypeError in Python. Unsupported operand type(s) for -: 'str' and 'str'...

Fix TypeError

'Age: ' + 25...

Learn how to resolve the TypeError in Python. Cannot concatenate string and integer. Use str(25)...

Fix TypeError

len(123)...

Learn how to resolve the TypeError in Python. Object of type 'int' has no len(). len() works on ...

Fix TypeError

list['a']...

Learn how to resolve the TypeError in Python. Type 'type' is not subscriptable (in older Python ...

Fix TypeError

None + 1...

Learn how to resolve the TypeError in Python. Unsupported operand type(s) for +: 'NoneType' and ...

Fix TypeError

raise 'Error'...

Learn how to resolve the TypeError in Python. Exceptions must derive from BaseException. Use rai...

Fix UnboundLocalError

x = 10; def foo(): print(x); x...

Learn how to resolve the UnboundLocalError in Python. Local variable 'x' referenced before assignment. U...

Fix UnicodeDecodeError

b'€'.decode('utf-8')...

Learn how to resolve the UnicodeDecodeError in Python. 'utf-8' codec can't decode byte 0x80. Check encodi...

Fix UnicodeEncodeError

'�'.encode('ascii')...

Learn how to resolve the UnicodeEncodeError in Python. 'ascii' codec can't encode character. Use 'utf-8'....

Fix ValueError

int('hello')...

Learn how to resolve the ValueError in Python. Invalid literal for int() with base 10. Ensure the...

Fix ValueError

list.remove(x)...

Learn how to resolve the ValueError in Python. x not in list....

Fix ZeroDivisionError

result = 10 / 0...

Learn how to resolve the ZeroDivisionError in Python. Division by zero is not allowed. Check your diviso...