Back to Blog
Developer guide
PYTHONJune 8, 2026

Understanding Python NameError with Real Examples

As Python developers, we've all encountered NameError at some point. But what does it mean, and how can we prevent it? In this article, we'll explore the causes of NameError, provide real-world examples, and offer practical solutions for fixing these errors. Whether you're a seasoned developer or just starting out, understanding NameError will help you write more robust and error-free code.

1. Undefined Variable

A NameError occurs when you try to access a variable that hasn't been defined.

Why It Happens

This error typically occurs when you've misspelled a variable name or forgotten to assign a value to a variable.

How to Fix It

To fix this error, ensure that you've defined the variable before using it. If the variable is supposed to be assigned a value within the code, verify that the assignment statement is executed before the variable is accessed.


2. Missing Module

A NameError also occurs when you try to import a module that doesn't exist.

Why It Happens

This error typically occurs when you've misspelled the module name or forgotten to install the required module.

How to Fix It

To fix this error, ensure that you've spelled the module name correctly and installed the required module using pip. You can also check if the module is installed by running pip list in your terminal.


3. Incorrect Function Call

A NameError occurs when you call a function that hasn't been defined.

Why It Happens

This error typically occurs when you've misspelled the function name or forgotten to define the function.

How to Fix It

To fix this error, ensure that you've defined the function before calling it. If the function is supposed to be defined within the code, verify that the function definition statement is executed before the function is called.


4. Import Statement Error

A NameError occurs when there's an issue with the import statement, such as a missing import or an incorrect import alias.

Why It Happens

This error typically occurs when you've misspelled the module name or used an incorrect import alias.

How to Fix It

To fix this error, ensure that you've spelled the module name correctly and used the correct import alias. You can also try removing the import statement and re-adding it to resolve any conflicts.


5. AttributeError with NameError

A NameError can occur when an AttributeError is raised, causing the name to be undefined.

Why It Happens

This error typically occurs when you're trying to access an attribute that doesn't exist in an object.

How to Fix It

To fix this error, ensure that the object has the attribute you're trying to access. You can check the object's documentation or use the dir() function to list its attributes.


6. Dynamic Variable Error

A NameError occurs when a dynamic variable is undefined.

Why It Happens

This error typically occurs when you've used a dynamic variable without defining it.

How to Fix It

To fix this error, ensure that you've defined the dynamic variable before using it. You can also use the globals() or locals() function to access dynamic variables.


7. Name Shadowing

A NameError can occur when a variable is shadowed by a local variable or function parameter.

Why It Happens

This error typically occurs when you've used a variable name that's the same as a local variable or function parameter.

How to Fix It

To fix this error, ensure that you're using a unique variable name that doesn't conflict with local variables or function parameters.


8. Type Error with NameError

A NameError can occur when a TypeError is raised, causing the name to be undefined.

Why It Happens

This error typically occurs when you're trying to access a variable that's not a string or has an incorrect type.

How to Fix It

To fix this error, ensure that the variable has the correct type. You can use the isinstance() function to check the type of the variable.

Conclusion

In conclusion, NameError is a common error in Python that can occur due to a variety of reasons, including undefined variables, missing modules, incorrect function calls, and attribute errors. By understanding the causes of NameError and following the solutions outlined in this article, you can write more robust and error-free code. Remember to always define variables before using them, use correct import statements, and check for attribute existence 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