Back to Blog
Developer guide
PYTHONMay 16, 2026

Understanding Python NameError with Real Examples

As a Python developer, you may have encountered the NameError exception at some point in your coding journey. This error occurs when the Python interpreter is unable to find or resolve a variable, function, or module that you are trying to use. In this article, we will delve into the world of NameError, exploring its causes, solutions, and providing real-world examples to help you understand and fix this common exception. Whether you're a beginner or an experienced developer, this article will help you navigate the complexities of NameError and improve your Python debugging skills.

1. Undefined Variable NameError

NameError is triggered when Python interpreter encounters an undefined variable. This occurs when you try to use a variable that hasn't been assigned a value or declared.

Why It Happens

This error is typically caused by a typo in the variable name, or using a variable before it's been assigned a value.

How to Fix It

To fix this error, ensure that the variable is properly assigned a value before using it. You can also use the print statement or a debugger to check the variable's value.


2. Unresolved Function NameError

NameError occurs when you try to call a function or method that doesn't exist in the current scope.

Why It Happens

This error is usually caused by a typo in the function name or using a function before it's been defined.

How to Fix It

To resolve this error, verify that the function is correctly defined and accessible in the current scope. You can also check for typos in the function name.


3. Missing Module NameError

NameError occurs when Python interpreter is unable to import a module that doesn't exist or isn't installed.

Why It Happens

This error is typically caused by a missing import statement or a typo in the module name.

How to Fix It

To fix this error, ensure that the module is correctly imported using the import statement. You can also check if the module is installed using pip or another package manager.


4. Undefined Class NameError

NameError is triggered when you try to use a class that hasn't been defined or imported.

Why It Happens

This error is usually caused by a typo in the class name or using a class before it's been defined.

How to Fix It

To resolve this error, verify that the class is correctly defined and imported in the current scope. You can also check for typos in the class name.


5. Shadowed Variable NameError

NameError occurs when a variable with the same name is redefined in an inner scope, causing the outer variable to be inaccessible.

Why It Happens

This error is typically caused by variable redefinition in an inner scope.

How to Fix It

To fix this error, avoid redefining variables in inner scopes. You can also use the outer variable's name with the global keyword to access it.


6. NameError with Built-in Functions

NameError is triggered when you try to use a built-in function with an incorrect or missing argument.

Why It Happens

This error is usually caused by incorrect or missing function arguments.

How to Fix It

To resolve this error, ensure that the correct number and type of arguments are passed to the built-in function. You can also refer to the function's documentation for guidance.


7. Dynamic Attribute NameError

NameError occurs when you try to access a dynamic attribute of an object that doesn't exist.

Why It Happens

This error is typically caused by incorrect or missing attribute names.

How to Fix It

To fix this error, ensure that the attribute name is correct and exists for the object. You can also use the hasattr function to check if the attribute exists.

Conclusion

In conclusion, NameError is a common exception that can occur in Python programming due to various reasons such as undefined variables, unresolved functions, missing modules, and more. By understanding the causes and solutions outlined in this article, you will be better equipped to identify and fix NameError in your code. Remember to always verify your variable and function names, check for typos, and use debuggers or print statements to troubleshoot issues. With practice and experience, you will become more proficient in debugging NameError and other exceptions in Python.

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