Back to Blog
Developer guide
PYTHONJune 30, 2026

Understanding Python NameError with Real Examples

Python is a popular programming language used in various applications, from web development to data analysis. However, even experienced developers can encounter errors while working with Python. One common error that developers face is the NameError, which occurs when Python encounters an undefined variable or object. In this article, we will delve into the world of NameError, covering its causes, and solutions with real examples to help you overcome this issue and become a more efficient Python developer.

1. Undefined variable or attribute

A NameError occurs when you try to use a variable or attribute that has not been defined. This can happen when you're working with variables, attributes, or even function parameters.

Why It Happens

This error occurs due to a simple mistake, such as misspelling a variable name or using a variable before it's defined.

How to Fix It

To fix this error, ensure that you've defined the variable or attribute you're trying to use before using it. You can do this by declaring the variable with 'var_name = value' or by accessing the attribute using the correct syntax.


2. Importing modules incorrectly

NameError can also occur when you're trying to import modules incorrectly. This can happen when you're using the wrong import statement or when the module you're trying to import does not exist.

Why It Happens

This error occurs when you're trying to import a module that does not exist or when you're using the wrong import statement. For example, you might try to import a module using a relative path instead of an absolute path.

How to Fix It

To fix this error, ensure that you're using the correct import statement. You can import modules using the 'import' statement or using relative paths. Additionally, make sure that the module you're trying to import exists in your Python path.


3. Using reserved keywords

NameError can also occur when you're using reserved keywords in Python. These keywords are used by Python to define its syntax and cannot be used as variable or function names.

Why It Happens

This error occurs when you're trying to use a reserved keyword as a variable or function name. Python will throw a NameError when it encounters this usage.

How to Fix It

To fix this error, avoid using reserved keywords as variable or function names. Instead, use a different name that does not conflict with Python's syntax.


4. Function or method not defined

NameError can also occur when you're trying to call a function or method that has not been defined. This can happen when you're working with functions or methods that are not yet implemented.

Why It Happens

This error occurs when you're trying to call a function or method that does not exist. This can happen when you're working with a new project or when you're trying to call a method that has not been implemented yet.

How to Fix It

To fix this error, ensure that you've defined the function or method you're trying to call. You can define functions using the 'def' statement and methods using the 'class' statement.


5. Class or object not defined

NameError can also occur when you're trying to access a class or object that has not been defined. This can happen when you're working with classes or objects that are not yet implemented.

Why It Happens

This error occurs when you're trying to access a class or object that does not exist. This can happen when you're working with a new project or when you're trying to access an object that has not been instantiated yet.

How to Fix It

To fix this error, ensure that you've defined the class or object you're trying to access. You can define classes using the 'class' statement and objects using the 'object' statement.


6. Missing or incorrect indentation

NameError can also occur when you're using incorrect or missing indentation in your Python code. This can happen when you're working with Python's syntax, which relies heavily on indentation to define block-level structure.

Why It Happens

This error occurs when you're using incorrect or missing indentation, which can cause Python to throw a NameError when it encounters this syntax.

How to Fix It

To fix this error, ensure that you're using correct indentation in your Python code. You can use the 'tab' or 'space' keys to indent your code and define block-level structure.

Conclusion

In conclusion, Python NameError is a common error that developers encounter while working with Python. By understanding the causes of NameError and following the solutions provided in this article, you can avoid this error and become a more efficient Python developer. Remember to define variables, avoid using reserved keywords, and ensure that you're using correct indentation in your Python code. With practice and patience, you'll become proficient in identifying and fixing Python NameError, making you a better developer in the long run.

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