AttributeError in Python — What It Means and How to Fix It

What Python's AttributeError exception means and how to resolve it.

Published October 27, 2026

AttributeError is raised when you access an attribute or method that an object does not have.

What it means and why it happens

Typical causes: a variable is None, a typo in the attribute name or the wrong object type.

Read the full message: it names the file and line where the problem was detected, which is the best place to start.

What you see

run the code
AttributeError: 'NoneType' object has no attribute 'strip'

Practical examples

Code that triggers it

name = None
print(name.strip())

The fix

name = None
print((name or '').strip())

Inspect the object

print(dir(obj))
print(hasattr(obj, 'strip'))

Common mistakes

  • Not checking for None.
  • Shadowing module names (random.py).
  • Wrong object type.

Key takeaways

  • AttributeError: AttributeError is raised when you access an attribute or method that an object does not have.
  • Read the message and the line number first.
  • Fix the cause, don't just silence the error.

FAQ

'module' object has no attribute?

You may have a local file named like the module, shadowing it.

How do I find which line caused AttributeError?

The error message and stack trace show the file and line; start there and work backwards.

More Python articles