Python: AttributeError: 'NoneType' object has no attribute
Python throws 'NoneType object has no attribute...' and points at a line that looks fine. The real cause is one step earlier: something you expected to hold a value is actually None. Here's how to find it.
Python stops with something like:
AttributeError: 'NoneType' object has no attribute 'append'
The trick to this one: the problem usually isn't the line in the error. None is Python's "nothing here" value, and this error means you called a method or accessed an attribute on a variable that turned out to be None. So the real question is: why is that thing None? — and the answer is almost always a step or two earlier.
The usual causes
Three patterns cause the overwhelming majority:
- A function with no
return. It runs, does its work, but hands backNoneby default — and you stored that. - A method that changes things in place and returns
None.list.sort(),list.append(), anddict.update()all mutate and return nothing.x = my_list.sort()makesxNone. - A lookup that missed.
dict.get('key')returnsNonewhen the key is absent; some library calls returnNoneon "not found."
Step 1: Find what's actually None
Read the error's attribute name — it tells you what kind of thing you expected. Then print the variable right before the failing line:
result = find_user(id)
print(repr(result)) # is it None? then find_user returned nothing
result.name # ← the AttributeError fires here
Step 2: Fix the source, not the symptom
If a function returns None when it shouldn't, add the return:
def double(x):
x * 2 # bug: computes but returns nothing
def double(x):
return x * 2 # fix
If you assigned the result of an in-place method, don't — call it, then use the original:
nums = [3, 1, 2]
nums.sort() # sorts nums in place; don't capture the return
print(nums) # [1, 2, 3]
"NoneType has no attribute X" is really "the thing I expected here is missing." Chase where it became None; the failing line is just where the emptiness got noticed.
Step 3: Guard when None is legitimate
If a value can legitimately be missing, check before you use it:
user = find_user(id)
if user is not None:
print(user.name)
The checklist
- The error means a variable is
None, not that the method is wrong. print(repr(x))just before the failing line to confirm what's None.- Trace back: missing
return? captured an in-place method? a.get()miss? - Fix the source; guard with
if x is not None:where None is valid.
Frequently Asked Questions
What does "'NoneType' object has no attribute 'find'" mean?
You called .find() on something that is None. It's common after a search that missed — for example a BeautifulSoup soup.find(...) that matches nothing returns None, and calling .find() again on that result fails. Check the object is not None before chaining another call.
Why is my variable None when I never set it to None?
Something handed it back to you as None: a function with no return, an in-place method like list.sort() that returns nothing, or a lookup like dict.get() that missed. Print the variable with repr() just before the failing line to catch which one.
How do I fix the AttributeError without hiding the real bug?
Guarding with if x is not None: stops the crash, but only reach for it when None is a legitimate value. If the variable should never be None, trace back to why it is — usually a missing return — and fix that instead.
Stop reading, start building
This pairs with a hands-on BytExplorer course — do it on your own machine and actually keep the skill.