NameError: name 'x' is not defined
Meaning, common causes, and fixes with examples.
What each error means, why it happens, and how to fix it—with short, runnable examples. Quick debugging for real code.
When Python stops your program and prints an error message, it is not trying to confuse you—it is telling you exactly where and why something went wrong. This section is a reference for the most common errors you will see as a beginner or as you move into real projects. Each article explains the error in plain English, shows a typical cause with runnable code, and gives one or more ways to fix it.
Errors fall into a few broad groups. Name errors mean you used a variable or name that Python does not know (for example a typo or a name used before it is defined)—see NameError and UnboundLocalError. Type errors mean you used a value of the wrong type: calling something that is not a function (e.g. int not callable, list not callable), mixing numbers and strings (int and str), or using None where a list or dict was expected (NoneType has no len(), NoneType not subscriptable, NoneType not iterable). Index and key errors happen when you access a position or key that does not exist—IndexError: list index out of range, string index out of range, KeyError, and KeyError with nested dicts. Value errors usually mean the value you passed is invalid for the operation, such as invalid literal for int() or could not convert string to float.
Syntax and indentation errors (SyntaxError, IndentationError, TabError) come from the shape of your code rather than runtime values. File and import problems (FileNotFoundError, ModuleNotFoundError, ImportError, I/O operation on closed file, PermissionError) are about the environment and how you open or load resources. Understanding these categories helps you know which link to open when you see a traceback.
Use the list below to jump to the exact error. If you are new to Python, start with Getting Started and Data types so that type and name errors make more sense in context.
Meaning, common causes, and fixes with examples.
When you get None instead of a sequence, and how to avoid it.
Why it happens and simple ways to fix or prevent it.
When a dictionary key doesn't exist, and how to handle it.
Trying to use a method or attribute on None.
When you try to convert a string that isn't a number to an integer.
Trying to add a number and a string together.
Python uses indentation to show code blocks—missing indentation causes this error.
Your code doesn't follow Python's grammar rules.
Python can't find the file you're trying to open.
The name you're trying to import doesn't exist in that module.
Python can't find the module you're trying to import.
You can't divide a number by zero—it's mathematically undefined.
Trying to call an integer like a function.
Using a variable before assigning it in a function.
A function called itself too many times without stopping.
Trying to call a list like a function.
Trying to use a list method on a string.
Trying to unpack more values than you have variables.
Trying to multiply a string or list by a float.
Trying to use indexing or slicing on None.
Trying to access a character position that doesn't exist in a string.
Trying to read or write to a file that's already been closed.
Trying to use square brackets on a function instead of calling it.
Trying to modify an immutable tuple.
Trying to get the length of None.
Trying to convert a string that isn't a valid number to a float.
Trying to call a dictionary like a function.
A key doesn't exist in a nested dictionary structure.
Trying to use indexing on a set, which doesn't support it.