Tutorials Logic, IN +91 8092939553 info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Interview Questions Website Development
Compiler Tutorials

NameError in Python — name is not defined Fix (2026) | Tutorials Logic

What is This Error?

The NameError occurs when you try to use a variable, function, or module name that Python doesn't recognize. This means the name hasn't been defined yet, is misspelled, or is out of scope.

Common Causes

  • Using a variable before defining it
  • Typo in variable or function name
  • Variable is out of scope (defined in different function/block)
  • Forgetting to import a module
  • Case sensitivity issues (Python is case-sensitive)

Quick Fix (TL;DR)

Quick Solution
# ❌ Problem
print(message)  # NameError: name 'message' is not defined

# ✅ Solution: Define the variable first
message = "Hello, World!"
print(message)

# ✅ Check for typos
username = "John"
print(username)  # Not 'userName' or 'user_name'

Common Scenarios & Solutions

Scenario 1: Using Variable Before Definition

The most common cause - trying to use a variable before it has been assigned a value.

Problem
print(total)  # NameError!
total = 100
Solution
total = 100  # Define first
print(total)  # Then use

Scenario 2: Typo in Variable Name

Misspelling a variable name or using incorrect case (Python is case-sensitive).

Problem
user_name = "Alice"
print(username)  # NameError: 'username' vs 'user_name'

firstName = "Bob"
print(firstname)  # NameError: case mismatch!
Solution
user_name = "Alice"
print(user_name)  # Exact match

firstName = "Bob"
print(firstName)  # Exact case match

# Use consistent naming convention (snake_case recommended)
user_name = "Alice"
first_name = "Bob"

Scenario 3: Variable Out of Scope

Trying to access a variable defined inside a function from outside, or vice versa.

Problem
def calculate():
    result = 100
    
print(result)  # NameError: result is local to calculate()
Solution
# Solution 1: Return the value
def calculate():
    result = 100
    return result
    
result = calculate()
print(result)

# Solution 2: Use global variable (not recommended)
result = 0

def calculate():
    global result
    result = 100
    
calculate()
print(result)

# Solution 3: Define outside function
result = 100

def calculate():
    print(result)  # Can read global variable
    
calculate()

Scenario 4: Forgetting to Import Module

Using a module or function without importing it first.

Problem
result = math.sqrt(16)  # NameError: name 'math' is not defined

data = json.loads('{"key": "value"}')  # NameError!
Solution
import math
result = math.sqrt(16)

import json
data = json.loads('{"key": "value"}')

# Or import specific functions
from math import sqrt
result = sqrt(16)

from json import loads
data = loads('{"key": "value"}')

Scenario 5: String Quotes Missing

Forgetting quotes around strings makes Python think it's a variable name.

Problem
name = Alice  # NameError: name 'Alice' is not defined
print(Hello)  # NameError!
Solution
name = "Alice"  # Add quotes for strings
print("Hello")  # Strings need quotes

Best Practices to Avoid This Error

  • Define before use - Always define variables before using them
  • Check spelling - Use autocomplete in your IDE to avoid typos
  • Be consistent with naming - Use snake_case for variables (PEP 8)
  • Import at the top - Put all imports at the beginning of your file
  • Use meaningful names - Avoid single letters except for loops
  • Use linters - Tools like pylint catch undefined names before runtime
  • Understand scope - Learn about local, global, and nonlocal variables

Related Errors

Key Takeaways
  • NameError occurs when using undefined variables, functions, or modules
  • Always define variables before using them in your code
  • Python is case-sensitive - "name" and "Name" are different variables
  • Import modules at the top of your file before using them
  • Variables defined inside functions are local and can't be accessed outside
  • Use IDE autocomplete and linters to catch undefined names early

Frequently Asked Questions


Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.