Back to Blog
Developer guide
PYTHONJune 26, 2026

Understanding Python NameError with Real Examples

Python NameError is a common error that occurs when a program tries to access a variable or attribute that has not been defined. As a Python developer, it's essential to understand what causes this error and how to fix it to write efficient and error-free code. In this article, we'll explore the different causes and solutions for Python NameError with real examples.

1. Undefined Variable NameError

This error occurs when you try to access a variable that has not been defined in your code.

Why It Happens

This error can occur due to a typo in the variable name or when you try to access a variable before it's defined.

How to Fix It

To solve this error, make sure to define the variable before accessing it. You can define a variable using the assignment operator (=). For example, if you try to access a variable 'my_variable' before defining it, you can fix the error by adding the line 'my_variable = None' before accessing it.


2. Missing Module NameError

This error occurs when you try to import a module that does not exist or has not been installed.

Why It Happens

This error can occur due to a missing import statement or when the module name is incorrect.

How to Fix It

To solve this error, make sure to install the required module using pip. You can use the 'pip install' command to install the module. If the module name is incorrect, check the documentation for the correct module name and update your import statement accordingly.


3. Attribute Error in NameError

This error occurs when you try to access an attribute of an object that does not exist.

Why It Happens

This error can occur due to a typo in the attribute name or when the object does not have the specified attribute.

How to Fix It

To solve this error, make sure to check the documentation for the correct attribute name and update your code accordingly. If the object does not have the specified attribute, you can add the attribute using the dot notation (object.attribute = value).


4. Shadowed Variable NameError

This error occurs when a variable is redefined in a smaller scope, causing the original variable to be inaccessible.

Why It Happens

This error can occur due to a variable being redefined in a function or loop.

How to Fix It

To solve this error, use the 'nonlocal' keyword to indicate that the variable is not local to the current scope. For example, if you have a variable 'my_variable' in the outer scope and redefine it in the inner scope, you can fix the error by adding the line 'nonlocal my_variable' in the inner scope.


5. Global Variable Access NameError

This error occurs when you try to access a global variable in a nested scope without using the 'global' keyword.

Why It Happens

This error can occur due to a missing 'global' statement or when the variable is not defined in the global scope.

How to Fix It

To solve this error, use the 'global' keyword to indicate that you want to access the global variable. For example, if you have a global variable 'my_variable' and try to access it in a nested scope without using the 'global' keyword, you can fix the error by adding the line 'global my_variable' in the nested scope.


6. Imported Module Not Found NameError

This error occurs when you try to import a module that does not exist in the import path.

Why It Happens

This error can occur due to a missing import statement or when the module name is incorrect.

How to Fix It

To solve this error, make sure to install the required module using pip. You can use the 'pip install' command to install the module. If the module name is incorrect, check the documentation for the correct module name and update your import statement accordingly.


7. UnboundLocal Variable NameError

This error occurs when you try to access a local variable that has not been assigned a value.

Why It Happens

This error can occur due to a missing assignment statement or when the variable is not defined in the local scope.

How to Fix It

To solve this error, make sure to assign a value to the variable before accessing it. You can use the 'assignment operator (=)' to assign a value to the variable. For example, if you have a local variable 'my_variable' and try to access it without assigning a value, you can fix the error by adding the line 'my_variable = None' before accessing it.

Conclusion

Python NameError is a common error that can occur due to various reasons such as undefined variables, missing modules, and attribute errors. By understanding the causes and solutions for these errors, you can write efficient and error-free code. Remember to define variables before accessing them, use the correct module names, and check the documentation for attribute names. With practice and experience, you'll become more proficient in debugging Python NameError and write high-quality code.

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