Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is a name error in Python

Understanding NameError in Python

When you're just starting out with programming, encountering errors can be a bit intimidating. But fear not! Errors are actually your friends. They're like signposts that guide you to understand what went wrong in your code, helping you learn and become a better programmer. One common error that you may come across in Python is the NameError. Let's delve into what this error means, why it occurs, and how you can resolve it.

What is a NameError?

Imagine you're in a room full of people, and you call out a name, expecting someone to respond. If there's no one by that name, you'll be met with silence or confused looks. Similarly, a NameError in Python occurs when you try to use a name (usually a variable or a function name) that Python doesn't recognize.

In technical terms, this error is raised when your code refers to a name that hasn't been defined yet. It's Python's way of saying, "I don't know what you're talking about!"

Common Causes of NameError

Misspelling a Name

One of the most common causes for a NameError is simply misspelling a variable or function name. This is like calling your friend "Alex" when their name is actually "Alec."

# Defining a variable
favourite_color = "blue"

# Trying to print the variable (misspelled)
print(favorite_color)  # This will raise a NameError

Using a Name Before Defining It

Another common mistake is trying to use a name before it has been defined. It's like trying to pour a cup of tea from a teapot that you haven't filled yet.

# Trying to print a variable before defining it
print(age)  # This will raise a NameError

# Defining the variable
age = 25

Scope Issues

Scope can be a tricky concept when you're new to programming. Think of scope like a fenced yard. If you have a dog named "Buddy" in your yard, only people inside the yard know who Buddy is. If someone outside the yard calls for Buddy, they won't get a response.

In Python, if you define a name inside a function, it's only recognized within that function, not outside of it.

def greet():
    message = "Hello, world!"

# Trying to print the message outside the function
print(message)  # This will raise a NameError

Forgetting to Import a Module

Sometimes, the name you're trying to use comes from a module (a file containing Python definitions and statements) that you need to import. It's like trying to read a book in the dark; you need to turn on the light (import the module) to read (use the name).

# Forgetting to import the 'math' module
print(math.sqrt(16))  # This will raise a NameError

How to Fix a NameError

Check for Typos

The first thing to do when you encounter a NameError is to check for typos. Make sure that you spelled the name correctly and that you're using the right case since Python is case-sensitive. "name" and "Name" are considered two different names in Python.

Define the Name Before Use

Make sure that you define a name before you try to use it. This means you should assign a value to a variable or define a function before you call or reference it.

Understand Scope

To avoid scope-related issues, remember that variables defined within a function are not accessible outside of it. If you need to use a variable both inside and outside of a function, you can define it outside the function or return it from the function.

Import Necessary Modules

If the name you're trying to use is part of a module, ensure that you've imported that module correctly using the import statement.

# Importing the 'math' module
import math

# Now you can use the 'sqrt' function from the 'math' module
print(math.sqrt(16))  # This will work correctly and output 4.0

Intuitions and Analogies

To better understand NameError, think of it as trying to read a book that isn't in your bookshelf. You can't read what you don't have. In the same way, Python can't use a name that hasn't been introduced. Always introduce your characters (variables and functions) to Python before you start telling the story (running the code).


Encountering a NameError in Python is like a rite of passage for beginners. It's a small bump in the road that, once understood, can be easily navigated. Remember, errors are not setbacks; they're stepping stones to mastering programming. Each NameError you fix brings you one step closer to fluency in Python. So next time you see one, greet it with a smile, roll up your sleeves, and show it who's boss. Happy coding!