Skip to content

AttributeError: object has no attribute

Quick summary

You tried to access obj.attribute or call obj.method() where the attribute or method does not exist on that object type.

Why this happens

  • The attribute or method name is misspelled or uses wrong casing (Python is case-sensitive)

  • The object is None or a different type than expected, and does not have the attribute you are accessing

  • The attribute was removed or renamed in a library version upgrade without updating your code

Minimal example

✗ Broken code
name = 'Hello'
print(name.upperr())  # AttributeError: 'str' object has no attribute 'upperr'
✓ Fixed code
name = 'Hello'
print(name.upper())  # correct method name, prints 'HELLO'

The method is 'upper' not 'upperr' — fixing the spelling makes the attribute exist on the str object.

How to diagnose

  • Print type(obj) to confirm the object type and dir(obj) to list all available attributes and methods

  • Check for None before accessing attributes with 'if obj is not None:' to avoid NoneType attribute errors

  • Use hasattr(obj, 'attribute') to test existence before accessing if the attribute may be optional

How to fix

  • Correct the attribute or method name spelling and casing to match the type's actual interface

  • Add a None check before accessing attributes on variables that might be None

  • Use getattr(obj, 'attribute', default) to return a default value when the attribute does not exist

How to prevent

  • Use type hints and a linter (mypy, pyright) to catch attribute access errors before runtime

  • Check for None explicitly and use hasattr() or getattr() for optional attributes on dynamic objects

Related Resources

Related Glossary

Related Lessons

← Back to language