TabError: inconsistent use of tabs and spaces in indentation
Error message
inconsistent use of tabs and spaces in indentation
Quick summary
Your file mixes tabs and spaces for indentation in a way that Python can't unambiguously interpret.
Why this happens
Mixing tabs and spaces in the same block
Copying code from different sources with different indentation
Editor settings changed between tab and space indentation
Minimal example
if True:
print('a') # tab
print('b') # spaces # TabErrorif True:
print('a') # 4 spaces
print('b') # 4 spacesThe original mixes a tab and spaces. Converting all indentation to 4 spaces resolves the ambiguity.
How to diagnose
Show whitespace characters in your editor
Run 'python -t file.py' to warn about tabs and spaces
Use a formatter like black to normalize indentation
How to fix
Convert all tabs to spaces (most editors have this option)
Use only spaces (4 per level, per PEP 8) for indentation
Run 'black file.py' to auto-format the file
How to prevent
Configure your editor to use spaces, not tabs
Set tab width to 4 and 'insert spaces for tabs'
Use a formatter like black in your workflow
Related Resources
Related Glossary
Related Lessons
- Python Overview
Indentation rules.