NameError: name is not defined (Fix)

Fix the Python error NameError: name is not defined. This page explains what the error means, why it happens, and the most common ways to correct it.

Quick fix

user_name = "Sam"
print(user_name)

This error happens when Python sees a name that has not been created yet, is misspelled, or is outside the current scope.

What this error means

NameError means Python found a variable, function, or module name it does not know.

In Python, a name can be:

  • A variable
  • A function
  • A module
  • Another object you assigned to a label

A name must exist before you use it. Python looks for that name in the current scope. If it cannot find it, it raises a NameError.

For example:

print(total)

Output:

NameError: name 'total' is not defined

Python stops because total does not exist yet.

Common example that causes the error

One of the most common causes is using a variable before assigning a value to it.

print(total)
total = 10

Output:

NameError: name 'total' is not defined

This fails because Python runs code from top to bottom. When it reaches print(total), total has not been created yet.

To fix it, define the variable first:

total = 10
print(total)

Output:

10

If you are new to variables, see Python variables explained for beginners.

Cause 1: Misspelled variable or function name

A small typo can cause NameError.

Check these carefully:

  • Uppercase and lowercase letters
  • Missing letters
  • Extra letters
  • Different naming styles

For example, userName and username are different names in Python.

username = "Sam"
print(userName)

Output:

NameError: name 'userName' is not defined

Fix:

username = "Sam"
print(username)

Make sure the name is spelled the same everywhere in your code.

Cause 2: Variable created after use

Assignment must happen before the name is used.

Wrong:

print(score)
score = 95

Right:

score = 95
print(score)

When debugging, read your code from top to bottom. Python does not look ahead to see that score will be created later.

This is a very common beginner issue when learning how variables work.

Cause 3: Name is outside the current scope

A variable inside a function is usually not available outside it.

def create_message():
    message = "Hello"

create_message()
print(message)

Output:

NameError: name 'message' is not defined

Here, message only exists inside the function.

To fix this, return the value and store it in a variable:

def create_message():
    message = "Hello"
    return message

result = create_message()
print(result)

Output:

Hello

A variable created in one function is also not automatically available in another function. In most cases, pass values as arguments or return them from functions.

If function scope is confusing, read Python functions explained.

Cause 4: Forgot to import a module or function

If you use a module name before importing it, Python will raise a NameError.

print(math.sqrt(16))

Output:

NameError: name 'math' is not defined

Fix:

import math

print(math.sqrt(16))

Output:

4.0

You can also import a specific function:

from math import sqrt

print(sqrt(16))

Output:

4.0

Check whether you imported the full module or a specific function. If you want to learn this properly, see how import works in Python.

Cause 5: Quotes missing around a string

This is a very common beginner mistake.

If you write this:

print(hello)

Python thinks hello is a variable name. Since no variable called hello exists, you get a NameError.

Output:

NameError: name 'hello' is not defined

To print text, use quotes:

print("hello")

Output:

hello

Use either single quotes or double quotes:

print('hello')
print("hello")

Both are valid.

How to fix it step by step

When you see NameError: name 'something' is not defined, use this checklist:

  1. Find the exact name shown in the error message.
  2. Check whether that name was created earlier in the code.
  3. Check spelling and letter case.
  4. Check whether the name is inside the correct function or block.
  5. Check imports if the name should come from a module.

These simple debugging commands can also help:

print(variable_name)
print(locals())
print(globals())
dir()
help(name)

Useful notes:

  • print(variable_name) helps confirm a value exists
  • print(locals()) shows names available in the current local scope
  • print(globals()) shows global names
  • dir() shows available names in the current context
  • help(name) gives information about an object if it exists

Be careful with print(variable_name): if variable_name does not exist, that line will also raise a NameError.

NameError is easy to confuse with other Python errors.

NameError

This means the name does not exist at all in the current scope.

print(color)

If color was never defined, Python raises NameError.

UnboundLocalError

This is a related scope problem that often happens inside functions.

count = 10

def show_count():
    print(count)
    count = 20

show_count()

This raises UnboundLocalError because Python treats count as a local variable inside the function, but it is used before assignment there.

For a full explanation, see UnboundLocalError: local variable referenced before assignment.

AttributeError

This happens when an object exists, but it does not have the attribute or method you asked for.

name = "Sam"
print(name.append("x"))

This raises AttributeError because strings exist, but they do not have an append() method.

See AttributeError: object has no attribute (Fix).

Common mistakes

These are the most common causes of this error:

  • Misspelled variable name
  • Using a variable before assignment
  • Case mismatch in a name
  • Using a local variable outside its function
  • Forgetting to import a module
  • Missing quotes around a string

If your error looks similar but not identical, you may also want to read NameError in Python: causes and fixes.

FAQ

What does NameError mean in Python?

It means Python cannot find the variable, function, or module name you used.

Why do I get NameError for a word I wanted to print?

You probably forgot quotes, so Python treated the word as a variable name instead of a string.

Is NameError the same as UnboundLocalError?

No. UnboundLocalError is a more specific scope-related error that often happens inside functions.

Can a typo cause NameError?

Yes. Even a small spelling difference or wrong capitalization can cause it.

See also