Ad Space — Top Banner

AttributeError

Python Programming Language

Severity: Moderate

What Does This Error Mean?

An AttributeError means you tried to access a property or method that does not exist on an object. Every type of object in Python has its own set of available attributes and methods. If you ask for one that is not available — whether due to a typo, a wrong type, or a None value — Python raises an AttributeError.

Affected Models

  • Python 2.x
  • Python 3.x
  • All Python versions

Common Causes

  • A typo in the method or attribute name (Python is case-sensitive — .Append() is not the same as .append())
  • Calling a method on the wrong type — for example, calling .split() on a number instead of a string
  • The variable is None (perhaps a function returned None instead of an object), and None does not have methods
  • Using a method that exists in a newer Python version than the one you are running
  • Accessing an attribute that you expected to exist on an object but was never set

How to Fix It

  1. Read the error message. It says something like 'NoneType object has no attribute X' or 'str object has no attribute X'. The object type tells you what went wrong.

    If it says NoneType, a function returned None instead of the expected object.

  2. Check for a typo in the attribute or method name. Python is case-sensitive. List methods are lowercase: .append(), .remove(), .sort() — not .Append() or .APPEND().

    Type the object name followed by a period in your editor and let autocomplete show you the valid options.

  3. If the error says 'NoneType object has no attribute', the variable is None. Trace back through your code to find where it was supposed to get a real value but did not.

    Common cause: a function with no return statement returns None. Check your functions end with a return statement.

  4. Use dir(object) in your Python console to see every attribute and method available on an object.

    Example: dir('hello') shows all string methods. This is much faster than guessing.

  5. Make sure you are calling the method on the right type. You cannot call string methods on a list or list methods on a number. Use type(variable) to confirm what you are working with.

    Example: if a variable is a list, use .append() to add items. Strings use .join() and .split() instead.

When to Call a Professional

AttributeErrors are always something you can fix yourself. The error message tells you the type of object and the attribute name that was not found. Use dir(object) to see all available attributes and methods for any object.

Frequently Asked Questions

Why does Python say 'NoneType has no attribute'?

This means a variable that should contain an object actually contains None. None is Python's way of representing 'no value'. A function returns None if it has no return statement, or if it explicitly returns None. Trace the variable back through your code to find where it lost its value.

How do I find out what methods are available on an object?

Use the built-in dir() function. For example, dir('hello') shows all string methods. dir([]) shows all list methods. You can also check the official Python documentation for the complete list.

What is the difference between an AttributeError and a NameError?

A NameError means Python cannot find the variable or function name at all — it was never defined. An AttributeError means Python found the object, but the specific property or method you asked for does not exist on it. NameError: the container does not exist. AttributeError: the container exists but not the item inside it.