NameError
This error occurs when the Python interpreter attempts to resolve a variable, function, or module name that has not been defined in the local or global scope.
Usually happens because:
- ☑ Typo or spelling mismatch inside referenced identifier string
- ☑ Forgetting to write the import statement for a module
- ☑ Variables defined inside local function scope accessed globally
🔍 Quick Checklist:
What is NameError?
A 'NameError' is a runtime error raised when Python fails to locate a matching identifier name in the local namespace, enclosing scopes, global namespace, or built-in namespaces. This commonly happens because of simple typographical spelling errors, referencing variables before they are initialized, neglecting to import modules before using them, or referencing variables defined inside function local scopes from parent global scopes.
Common Causes
- Spelling typo in variable or function name: Simple spelling mistakes in referencing identifiers.
- Missing module imports: Calling library tools (like 'math.sin' or 'os.path') without writing the 'import' statement first.
- Variable scope boundary mismatch: Attempting to reference local variables outside their parent function boundaries.
| Cause | Frequency |
|---|---|
| Spelling typo in identifier name (variable/function) | ⭐⭐⭐⭐⭐ |
| Neglecting to import module namespace (e.g. forgot import os) | ⭐⭐⭐⭐ |
| Accessing local scope variables in global context | ⭐⭐⭐ |
Common Mistakes
- Using a variable inside loops (like list comprehensions) that name-clashes with global variables, creating scope shadowing.
- Forgetting to import libraries like `os` or `sys` before referencing their methods, causing NameErrors on scripts startups.
How to Fix
Python Operations & Verification
Use the global keyword to declare that modifications inside a function target a global variable.
counter = 0
def increment():
# Tell Python we are writing to the global counter variable
global counter
counter += 1
increment()
print(counter) # 1Platform Specific Fixes
Verify import syntax statements using quick testing lines.
python3 -c "import sys; print(sys.argv)"Best Practices
- Run static code linters (like PyFlakes or Flake8) to detect undefined names before execution.
- Declare and initialize all variables at the beginning of classes or code scopes.
Frequently Asked Questions (FAQ)
Q: What is a NameError?
It is a runtime exception raised when Python cannot find a variable or function name you referenced in your code.
Q: How do I fix 'NameError: name 'X' is not defined'?
First check for typos. If the spelling is correct, ensure that the variable 'X' is assigned a value before you try to read it, or that you have imported the module hosting it.
Q: Why can't I access variables defined inside a function?
Variables defined inside functions have 'local scope' and cannot be accessed from the outside. To use them, return the value from the function or declare the variable in the global scope.
Q: How do I access a global variable from inside a function?
To read a global variable, you can reference it directly. To modify a global variable inside a function, declare it using the 'global' keyword: 'global my_var'.