TL;DR
You wrote a colon (:) but left the block below it empty. Python can't proceed without at least one statement there. Either add the actual code, or drop in a pass as a placeholder. Also check that you're not mixing tabs and spaces β Python 3 treats that as a hard error.
What Triggers This Error
In Python, a colon isn't just punctuation β it's a promise. Every statement ending with : must be followed by an indented block. This applies to if, else, elif, for, while, def, class, with, and try/except/finally.
Skip the body, and Python throws:
IndentationError: expected an indented block
Common Scenarios and Fixes
1. Empty function or class body
You stub out a function or class but haven't written the body yet:
# Bad
def my_function():
class MyClass:
Fix β add pass:
# Good
def my_function():
pass
class MyClass:
pass
2. Comment-only body
Comments don't count as statements. Python still sees an empty block β that # TODO might as well not be there:
# Bad
def process():
# TODO: implement this
def main():
# coming soon
Fix β pair the comment with pass:
# Good
def process():
# TODO: implement this
pass
3. if/else with missing body
# Bad
if condition:
else:
do_something()
Fix:
# Good
if condition:
pass
else:
do_something()
4. try/except with empty block
# Bad
try:
except ValueError:
handle_error()
Fix:
# Good
try:
risky_call()
except ValueError:
handle_error()
5. Mixed tabs and spaces
This one's easy to miss. The code looks indented in your editor, but one line uses a tab while another uses spaces. Python 3 rejects this outright. You might see a TabError, but it sometimes surfaces as an IndentationError instead.
# Bad (tab on one line, spaces on another)
def greet():
print("hello") # tab
print("world") # spaces
Standardize to spaces β 4 per level, as PEP 8 recommends:
def greet():
print("hello")
print("world")
To bulk-convert in your editor: VS Code β Command Palette β "Convert Indentation to Spaces". In vim, run :retab. From the terminal:
expand -t 4 myfile.py > myfile_fixed.py
6. Accidentally un-indented block
# Bad
for item in items:
print(item) # not indented
Fix:
# Good
for item in items:
print(item)
Reading the Traceback
Python's error message points straight at the problem:
File "script.py", line 5
def helper():
^
IndentationError: expected an indented block
Line 5 is where Python expected a body to begin. Look at the line before the caret β that's your colon. The block below it is empty or indented wrong.
Quick Checklist
- Every
:at the end of a line must be followed by an indented block on the next line. - Empty blocks need
pass. - A comment alone doesn't satisfy the block requirement β add
passbelow it. - Use spaces only β 4 per level. Set your editor to insert spaces when you press Tab.
- Run
python -m py_compile yourfile.pyto catch syntax errors before executing.
Verifying the Fix
Run a quick syntax check without executing the file:
python -m py_compile script.py
echo $? # 0 means clean
For broader style and error checking, flake8 covers more ground:
pip install flake8
flake8 script.py
No output, no problems. If py_compile exits with 0, the IndentationError is gone.
Editor Settings to Prevent This
- VS Code: set
"editor.tabSize": 4and"editor.insertSpaces": trueinsettings.json. - PyCharm: already defaults to 4-space indentation for Python files.
- Vim: add
autocmd FileType python setlocal expandtab shiftwidth=4 tabstop=4to.vimrc.

