Skip to content

Name Error

Quick summary

You referenced a name (variable, function, class) that doesn't exist in the current scope.

Why this happens

  • Typo in the variable or function name

  • Forgot to define or import the name before using it

  • Used a variable outside its scope (e.g., a local variable outside its function)

Minimal example

✗ Broken code
print(count)
✓ Fixed code
count = 0
print(count)

count is never assigned a value before it is used — define it first.

How to diagnose

  • Check the spelling of the name against its definition

  • Verify the name is defined or imported before the line that uses it

  • Check the scope — is the name visible where you're using it?

How to fix

  • Fix the typo to match the defined name

  • Define or import the name before using it

  • Move the code into the correct scope or make the name accessible

How to prevent

  • Use an IDE with autocomplete to avoid typos

  • Enable linters that catch undefined names (ESLint no-undef, flake8)

Related Resources

Related Glossary

Related Lessons

  • Variables

    Learn how variables work in programming

Related Practice

← Back to language