IndentationError: unexpected indent means a line is indented further than Python expects at that point, usually a stray space or tab at the start of a line that isn't inside a block. Remove the extra indentation so the line lines up with the code around it, or, if it should be inside a block, add the missing line ending in a colon above it.
Python uses indentation instead of braces to group statements, so the amount of whitespace at the start of a line is part of the syntax. There are four related messages, and each points to a different mistake. All the examples below were run on Python 3.12; paths are shortened to C:\work.
The four indentation errors
| Message | What's wrong | Fix |
|---|---|---|
IndentationError: unexpected indent | A line is indented but nothing opened a block | Remove the indentation, or add the missing if/for/def line |
IndentationError: expected an indented block after ... | A line ending in : has no indented body | Indent the body, or put pass there |
IndentationError: unindent does not match any outer indentation level | A dedent lands between two existing levels | Line it up exactly with an outer block |
TabError: inconsistent use of tabs and spaces in indentation | Tabs and spaces mixed in the same block | Convert the file to spaces |
IndentationError: unexpected indent
Code:
x = 1
y = 2
Output:
File "C:\work\a.py", line 2
y = 2
IndentationError: unexpected indent
Line 2 is indented, but line 1 doesn't end with a colon, so no block was opened. Either remove the four spaces before y, or, if y = 2 really belongs inside a condition or loop, add that line:
x = 1
if x > 0:
y = 2
Common causes: code copied from a web page or chat with leading spaces, a line pasted from inside a function to the top level, or a leftover space at the start of a line that's invisible in your editor. Most editors have an option to show whitespace characters, which makes these obvious.
IndentationError: expected an indented block
def f():
return 1
File "C:\work\b.py", line 2
return 1
^
IndentationError: expected an indented block after function definition on line 1
Python 3.12 names the statement that's missing its body ("after function definition on line 1"), which tells you where to look. Indent the body. If you're writing a placeholder, comments alone don't count as a body: this still raises the same error, because Python ignores comment-only lines when reading indentation.
def f():
# todo
Use pass (or ...) as the placeholder:
def f():
pass # todo
IndentationError: unindent does not match any outer indentation level
for i in range(3):
print(i)
print("done")
File "C:\work\g.py", line 3
print("done")
^
IndentationError: unindent does not match any outer indentation level
This one comes straight from how Python tracks indentation. The language reference describes a stack of indentation levels, starting at 0. A deeper line pushes a new level; a shallower line must match one of the levels already on the stack, and everything above it is popped. Here the stack is 0 and 4, and line 3 is at 2, which is neither. Python can't tell whether you meant the line to be inside the loop (4 spaces) or after it (0), so it stops.
Fix it by choosing one: 4 spaces to keep it in the loop, 0 to run it once afterwards. The caret points past the end of the line; go by the line number.
TabError: inconsistent use of tabs and spaces
Here lines 2 and 3 are indented with tabs, line 4 with 8 spaces (shown as \t for tabs):
def f():
\tif True:
\t\treturn 1
return 2
File "C:\work\d.py", line 4
return 2
TabError: inconsistent use of tabs and spaces in indentation
In most editors this code looks perfectly aligned, which is what makes it confusing. The language reference explains the rule: tabs are expanded to the next multiple of 8 columns, but Python rejects the file if the meaning of the indentation would depend on how wide a tab is. Line 4's 8 spaces would equal one tab at width 8 but two tabs at width 4, so the result is ambiguous and Python refuses. TabError is a subclass of IndentationError, which is itself a subclass of SyntaxError.
The fix is to use one kind of whitespace. PEP 8 recommends 4 spaces per level and says "Python disallows mixing tabs and spaces for indentation."
Find the bad line with tabnanny
The standard library includes a checker for exactly this. Point it at a file or a folder:
python -m tabnanny d.py
d.py 4 ' return 2'
It prints the file, the line number and the line itself, quoted so the leading whitespace is visible. Give it a folder and it checks every .py file inside, recursively; -q prints only the names of files with problems, and -v prints a fuller explanation. To check that a file compiles without running it, python -m py_compile d.py exits with status 1 and prints Sorry: TabError: inconsistent use of tabs and spaces in indentation (d.py, line 4).
How to fix indentation for the whole file
- Your editor: most editors can convert a file's tabs to spaces. In VS Code the detected indentation (for example "Spaces: 4" or "Tab Size: 4") is shown on the right of the status bar; clicking it offers commands to convert between tabs and spaces, and the
editor.insertSpacessetting controls what new lines get. - A formatter, once the file parses:
ruff format(orblack) rewrites a file with 4-space indentation, so a file indented only with tabs comes out with spaces. It can't fix a file Python can't parse: on the mixed-indentation example above,ruff format0.15 stopped witherror: Failed to parse d2.py:4:1: Unexpected indentation. Fix the reported line by hand first. - Command line: on Linux and macOS,
expand -t 4 old.py > new.pyconverts tabs to spaces. Check the result: a tab width of 4 is a guess about how the file was written.
Errors that look like indentation problems but aren't
Text copied from web pages, PDFs and chat apps can contain a non-breaking space (U+00A0) instead of a normal space. Python treats it as an illegal character, not as indentation:
File "C:\work\nb.py", line 3
� x = 1
^
SyntaxError: invalid non-printable character U+00A0
Delete the leading whitespace on that line and retype it with the space bar. If many lines are affected, use your editor's find-and-replace to replace U+00A0 with a normal space.
An unclosed bracket is another lookalike. Inside brackets, indentation of continuation lines doesn't matter, so the lines that follow are swallowed into one long expression. Python 3.12 reports it directly as SyntaxError: '(' was never closed and points at the opening bracket, so if you see that, the indentation is fine. The guide on how to read a Python traceback covers how to read the location lines in these messages.
IndentationError when pasting into the Python shell
The classic interactive interpreter treats a blank line as the end of a block; the Python tutorial says a compound statement entered interactively "must be followed by a blank line to indicate completion". Paste a function that has a blank line in the middle, and the shell finishes the function at that blank line. The next indented line then arrives with no block open: unexpected indent.
Python 3.13 replaced the default shell with one that supports multiline editing and a paste mode: press F3, paste, then press F3 again. On 3.12 and earlier, the reliable fix is to save the code to a .py file and run it with python file.py, or remove the blank lines inside blocks before pasting.
Frequently asked questions
- Is IndentationError a runtime error?
- No. IndentationError is a subclass of SyntaxError, raised while Python is reading the file, before any line runs. That is why a script with an indentation problem prints nothing at all except the error, even if the bad line is near the end of the file.
- Do comments and blank lines need to be indented correctly?
- No. Python ignores lines that contain only whitespace or only a comment when it works out indentation, so a comment at the wrong indent does not cause an error. A body that contains nothing but comments, however, still counts as missing, and you get 'expected an indented block'. Put pass in it.
- Should I use tabs or spaces in Python?
- Spaces. PEP 8, the official style guide, says to use 4 spaces per indentation level and that spaces are the preferred method, with tabs only to stay consistent with code already indented with tabs. Python itself refuses to run code that mixes the two in an ambiguous way.
- Why does pasted code give IndentationError in the Python shell?
- The classic interactive interpreter ends a block at the first blank line, so pasting a function with blank lines inside it splits the function and the next indented line becomes an unexpected indent. Python 3.13's new shell has a paste mode on F3 for this; on older versions, save the code to a file and run it instead.
Sources
- Python docs: Lexical analysis, Indentation
- Python docs: Built-in exceptions (IndentationError, TabError)
- PEP 8: Style Guide for Python Code, Indentation
- Python docs: An Informal Introduction to Python (interactive mode)
- Python docs: What's New in Python 3.13 (new interactive interpreter)
- Python docs: tabnanny
- Visual Studio Code docs: Basic editing (indentation)