Back to Blog
Developer guide
PYTHONJuly 6, 2026

Understanding Python NameError with Real Examples

Python NameError is a common exception that occurs when Python tries to access a variable, function, or attribute that doesn't exist. As a developer, it's crucial to understand the causes and solutions to this error to efficiently debug your code. In this article, we'll delve into the world of Python NameError and provide real examples to help you resolve this issue.

1. Undefined Variable

This error occurs when Python tries to access a variable that hasn't been defined or assigned a value.

Why It Happens

The most common cause of this error is missing or incorrect variable assignment.

How to Fix It

Check if the variable has been assigned a value before trying to access it. Make sure to use the correct variable name and check for typos.


2. Missing Module

This error occurs when Python tries to import a module that doesn't exist or can't be found.

Why It Happens

The most common cause of this error is incorrect module name or missing import statement.

How to Fix It

Check the module name and ensure it's correct. Verify that the module is installed and available for import. You can use the `pip` tool to install missing modules or check if they're part of the standard library.


3. Undefined Function

This error occurs when Python tries to call a function that hasn't been defined or assigned a value.

Why It Happens

The most common cause of this error is missing or incorrect function definition.

How to Fix It

Check if the function has been defined and assigned a value before trying to call it. Make sure to use the correct function name and check for typos.


4. AttributeError

This error occurs when Python tries to access an attribute of an object that doesn't exist or can't be accessed.

Why It Happens

The most common cause of this error is incorrect attribute name or missing object initialization.

How to Fix It

Check the attribute name and ensure it's correct. Verify that the object has been properly initialized and has the expected attributes.


5. Shadowed Variable

This error occurs when a variable is reassigned a different value, causing Python to lose track of the original variable.

Why It Happens

The most common cause of this error is using the same variable name in a different scope.

How to Fix It

Use a different variable name in the inner scope to avoid shadowing the original variable. Alternatively, use a global keyword to access the original variable.

Conclusion

In conclusion, Python NameError is a common exception that can be caused by various factors such as undefined variables, missing modules, and attribute errors. By understanding the causes and solutions to these errors, you can efficiently debug your code and resolve NameError issues. Remember to check variable assignment, module import, function definition, and attribute access to avoid NameError.

Explore More Debugging Resources

- [Browse all PYTHON errors](/languages/python)

- [Browse errors by type](/error-types)

- [Search all documented errors](/search)

- [Use the Error Explainer](/error-explainer-tool)

Browse allPython errors

Related PYTHON Articles

Have a specific error? Get an instant explanation.

Explain an Error