NameError: global name not defined (Fix)
Fix the Python error "NameError: global name not defined" by finding the missing or misspelled name and making sure it exists before you use it.
Quick fix
message = "Hello"
print(message)
A NameError happens when Python cannot find a variable, function, or module name. Define it first, spell it correctly, and use the right scope.
What this error means
Python raises this error when it tries to use a name that does not exist in the current scope.
A name can be:
- A variable
- A function
- A class
- An imported module
You will often see this in modern Python 3 as:
NameError: name 'message' is not defined
Older Python 2 wording sometimes says "global name not defined". The meaning is the same.
Why it happens
This error usually happens for one of these reasons:
- You used a variable before assigning a value to it
- You misspelled the name
- You used a name outside the function or block where it was created
- You forgot to import a module or function
- You expected a global variable to exist, but it was never defined
If you are new to variables, see Python variables explained for beginners.
Example that causes the error
Here are common examples that trigger NameError.
Using a variable before assigning it
print(total)
total = 10
Output:
NameError: name 'total' is not defined
Python reads code from top to bottom. At the moment print(total) runs, total does not exist yet.
Forgetting to import a module
print(math.sqrt(4))
Output:
NameError: name 'math' is not defined
Fix:
import math
print(math.sqrt(4))
Output:
2.0
If imports are confusing, read Python modules explained.
Using the wrong variable name
user_name = "Maya"
print(username)
Output:
NameError: name 'username' is not defined
The variable is named user_name, not username.
How to fix it
Use these steps to fix the error:
- Define the variable before you use it
- Check spelling and letter case carefully
- Make sure the name exists in the right scope
- Import the module or function before calling it
- Read the traceback line number to find where the missing name is used
Example of fixing the order:
total = 10
print(total)
Example of fixing an import:
import math
result = math.sqrt(9)
print(result)
Example of fixing a typo:
username = "Maya"
print(username)
Check scope problems
Scope means where a name is available in your program.
- A variable created inside a function usually cannot be used outside that function
- A variable created outside a function can usually be read inside the function
- If you assign to a name inside a function, Python treats it as local unless handled differently
Local variable used outside a function
def greet():
message = "Hello"
greet()
print(message)
Output:
NameError: name 'message' is not defined
Why this happens:
messagewas created insidegreet()- It only exists while that function runs
- Outside the function, Python cannot find it
Fix it by returning the value:
def greet():
message = "Hello"
return message
message = greet()
print(message)
Output:
Hello
If you want a fuller explanation, see Python variables explained for beginners and UnboundLocalError: local variable referenced before assignment.
Debugging steps
When you see this error, work through these steps:
- Read the exact missing name in the error message
- Go to the line shown in the traceback
- Search earlier in the file for where that name should be defined
- Check for typos, missing imports, and wrong indentation
- Print nearby values if needed to confirm your program flow
Useful debugging commands:
print(variable_name)
print(locals())
print(globals())
For example, if you think a variable should exist inside a function:
def test():
value = 5
print(locals())
test()
print(globals())
locals()shows names available in the current local scopeglobals()shows names available at the top level of the file
You can also run your script from the terminal to see the full traceback:
python your_script.py
If you want a step-by-step debugging process, read How to debug Python code.
Common mistakes
These are the most common causes of this error:
- Using a variable before assignment
- Misspelled variable or function name
- Wrong uppercase or lowercase letters
- Forgotten import statement
- Trying to use a local variable outside its function
- Assuming a global variable already exists
Remember that Python is case-sensitive. These are different names:
count = 5
print(Count)
This causes:
NameError: name 'Count' is not defined
Because count and Count are not the same.
FAQ
Is this the same as NameError: name is not defined?
Yes. "global name not defined" is older wording. Both mean Python cannot find a name.
Can a typo really cause this error?
Yes. Even a small spelling difference or wrong capitalization can cause NameError.
Why does this happen inside a function?
The function may be using a name that was never passed in, never defined, or only exists in another scope.
How do I fix a missing module name?
Import the module first, such as import math, before using math.sqrt().