Skip to content

python-beginner-help/python-error-fixes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

Python Beginner Help

Python Error Fixes 🐍🔧

A searchable index of the Python errors beginners hit most — what they mean, why they happen, and how to fix them.

Paste your error message into your editor's search (or use Ctrl/Cmd+F on this page) and jump to the fix. Each entry links to a full, step-by-step walkthrough on pythonbeginner.help.

How to read a Python traceback

Python prints the last line of a traceback as the error type and message — read it bottom-up:

Traceback (most recent call last):
  File "app.py", line 3, in <module>
    print(items[5])
          ~~~~~^^^
IndexError: list index out of range

The error type (IndexError) tells you the category; the message (list index out of range) tells you the specifics. Find your type below.

Error index

Error Typical message Common cause Fix
AttributeError 'NoneType' object has no attribute 'x' Using the result of a function that returned None Fix →
AttributeError 'list' object has no attribute 'x' Calling a string/dict method on a list Fix →
AttributeError 'str' object has no attribute 'x' Treating a string like a list or number Fix →
IndexError list index out of range Index ≥ length of the list Fix →
IndexError tuple index out of range Index past the end of a tuple Fix →
KeyError 'somekey' Reading a dict key that doesn't exist Fix →
IndentationError unexpected indent A line is indented when it shouldn't be Fix →
IndentationError expected an indented block A block (after :) is empty or not indented Fix →
ImportError No module named 'x' Package not installed or wrong name Fix →
ImportError cannot import name 'x' Name doesn't exist / circular import Fix →
FileNotFoundError [Errno 2] No such file or directory Wrong path or working directory Fix →
AssertionError (often blank) An assert condition was False Fix →

Quick fixes by example

IndexError: list index out of range

items = ["a", "b", "c"]
print(items[3])   # ✗ valid indexes are 0, 1, 2
items = ["a", "b", "c"]
if len(items) > 3:
    print(items[3])   # ✓ check the length first

Full explanation →

KeyError: 'name'

user = {"id": 1}
print(user["name"])        # ✗ key 'name' is missing
user = {"id": 1}
print(user.get("name"))    # ✓ returns None instead of crashing
print(user.get("name", "Anonymous"))  # ✓ with a default

Full explanation →

AttributeError: 'NoneType' object has no attribute ...

result = my_list.sort()   # sort() returns None, not the list
result.append(5)          # ✗ result is None
my_list.sort()            # ✓ sorts in place
my_list.append(5)         # ✓ use the list itself

Full explanation →

More

License

Released under CC0 1.0 by pythonbeginner.help — use it however you like.

About

Searchable index of common Python errors and how to fix them, for beginners. Powered by pythonbeginner.help

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors