AttributeError in Python: Causes and Fixes

AttributeError happens when Python cannot find an attribute or method on an object.

This usually means one of these is true:

  • You used a method on the wrong type of value
  • You misspelled the attribute name
  • Your variable is None
  • The variable changed to a different type earlier in the code

A common example is trying to use .split() on a list instead of a string.

Quick fix

Start by checking what the variable really is:

value = [1, 2, 3]

print(type(value))
print(dir(value))

# Check the method name and make sure the object type supports it

Most AttributeError problems happen because you called a method or attribute on the wrong type, used the wrong name, or got None instead of the object you expected.

If you are new to debugging, this pairs well with a beginner guide on how to debug Python code.

What AttributeError means

Python raises AttributeError when you try to use an attribute or method that an object does not have.

A few important points:

  • An attribute can be a value like user.name
  • An attribute can also be a method like text.upper()
  • The error message usually tells you:
    • the object type
    • the missing attribute name

Example error message:

AttributeError: 'list' object has no attribute 'split'

In that example:

  • the object type is list
  • the missing attribute is split

Why this error happens

AttributeError usually happens for one of these reasons:

  • You used a method that belongs to a different type
  • You made a spelling mistake in the attribute name
  • A variable contains None, not the object you expected
  • You reassigned a variable to a new type by mistake
  • You expected a module, class, or object to have a name that does not exist

Common causes include:

  • Calling a string method on a list
  • Calling a list method on a string
  • Using the wrong capitalization in a method name
  • Trying to access an attribute on None
  • Reassigning a variable to a different type
  • Using an attribute that does not exist on a module or custom object

Example that causes AttributeError

Here is a short example that fails:

values = ["a", "b", "c"]
print(values.split(","))

Output:

AttributeError: 'list' object has no attribute 'split'

Why this happens:

  • values is a list
  • .split() is a string method
  • Lists do not have a .split() method

If you are dealing with this exact error, see how to fix 'list' object has no attribute errors.

Fix 1: Use the correct method for the object type

First, check the type of the value:

value = ["apple", "banana", "orange"]
print(type(value))

Output:

<class 'list'>

Now use a method that belongs to a list, not a string:

value = ["apple", "banana", "orange"]
value.append("grape")
print(value)

Output:

['apple', 'banana', 'orange', 'grape']

If you actually need string behavior, use a string:

text = "apple,banana,orange"
parts = text.split(",")
print(parts)

Output:

['apple', 'banana', 'orange']

The key idea is simple:

  • If the value is a string, use string methods
  • If the value is a list, use list methods

You can also use type() in Python to confirm what kind of object you have.

Fix 2: Check for spelling mistakes

Attribute names are case-sensitive.

For example, this is wrong:

text = "hello"
print(text.Upper())

Output:

AttributeError: 'str' object has no attribute 'Upper'

The correct method name is .upper():

text = "hello"
print(text.upper())

Output:

HELLO

Small typos often cause this error.

To inspect what names an object supports, use dir():

text = "hello"
print("upper" in dir(text))
print("Upper" in dir(text))

Output:

True
False

Fix 3: Watch for None values

A very common version of this error is:

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

This means your variable is None.

Example:

def get_name():
    print("Running function")
    # No return statement

name = get_name()
print(name.upper())

Output:

Running function
Traceback (most recent call last):
  ...
AttributeError: 'NoneType' object has no attribute 'upper'

Why this happens:

  • get_name() does not return anything
  • A function with no return gives None
  • None does not have an .upper() method

One fix is to return a value:

def get_name():
    return "sam"

name = get_name()
print(name.upper())

Output:

SAM

Another fix is to check before using the value:

name = None

if name is not None:
    print(name.upper())
else:
    print("name is None")

If this is your exact problem, see how to fix 'NoneType' object has no attribute errors.

Fix 4: Trace where the variable changed

Sometimes a variable starts as one type and later becomes another type.

Example:

value = "1,2,3"
print(type(value))

value = [1, 2, 3]
print(type(value))

print(value.split(","))

Output:

<class 'str'>
<class 'list'>
Traceback (most recent call last):
  ...
AttributeError: 'list' object has no attribute 'split'

The problem is not only the last line. The real problem is that value changed from a string to a list.

To debug this:

  • Print the value before the failing line
  • Print type(value)
  • Look for earlier reassignment
  • Use clearer variable names

For example, this is easier to understand:

text = "1,2,3"
numbers = [1, 2, 3]

print(text.split(","))
print(numbers)

You can also use isinstance() in Python when you need to check types during debugging.

How to debug AttributeError step by step

When you see AttributeError, follow this process:

  1. Read the full error message carefully
  2. Identify the object type named in the message
  3. Identify the missing attribute name
  4. Print the variable and type(variable)
  5. Use dir(variable) to see available attributes
  6. Check the documentation or your class definition if needed

Useful debugging commands:

print(value)
print(type(value))
print(dir(value))
print(hasattr(value, "attribute_name"))

Example:

value = 100

print(value)
print(type(value))
print(hasattr(value, "append"))
print(dir(value))

This helps you answer questions like:

  • Is value really the type I expected?
  • Does this object actually have the method I want?
  • Did I misspell the attribute?

If you want a broader overview, read Python errors and exceptions explained.

Common AttributeError examples

Here are some common forms of this error:

  • 'list' object has no attribute ...
  • 'str' object has no attribute ...
  • 'NoneType' object has no attribute ...
  • 'int' object has no attribute ...
  • module has no attribute ...

Examples:

  • A list used like a string
  • A string used like a list
  • An integer used like an object with string or list methods
  • A module name used with an attribute that does not exist

For object-specific help, see:

How to prevent AttributeError

You can prevent many AttributeError problems with a few habits:

  • Learn the basic methods for strings, lists, dictionaries, and sets
  • Check return values from functions
  • Avoid reusing one variable name for different types
  • Use type() and isinstance() when debugging
  • Write small tests for important functions

Good habits make a big difference:

  • Use clear variable names
  • Print values when something looks wrong
  • Read the error message fully
  • Check method names carefully

FAQ

What is an attribute in Python?

An attribute is a name attached to an object. It can be a stored value like user.name or a method like text.lower().

What is the difference between AttributeError and NameError?

NameError means a variable name does not exist. AttributeError means the object exists, but the attribute or method does not.

Why do I get 'NoneType' object has no attribute?

Your variable is None, and None does not have the attribute you tried to use. This often happens when a function returns nothing.

How do I see what attributes an object has?

Use dir(object) to list available attributes and methods. For a quick type check, use type(object).

See also