What this error means
Python found both tabs and spaces used for indentation in the same file. Python requires consistent indentation—you must use either tabs or spaces throughout, not both.
Common causes
- You copied code from different sources that used different indentation.
- Your editor is set to insert tabs, but some lines have spaces.
- You manually mixed tabs and spaces while editing.
Example that causes the error
def greet():
print("Hello") # Uses spaces
print("World") # Uses tab (mixed!)
How to fix it: use spaces consistently
Python style guide (PEP 8) recommends using 4 spaces. Convert all tabs to spaces:
def greet():
print("Hello") # 4 spaces
print("World") # 4 spaces (consistent)
Configure your editor
Most editors can convert tabs to spaces automatically:
- VS Code: Settings → "Convert Indentation to Spaces"
- PyCharm: Settings → Editor → Code Style → Python → Use spaces
- Vim:
:set expandtab
Use Python to fix it
# Run this to convert tabs to spaces:
python -m tabnanny your_file.py # Shows where tabs are
# Then use your editor's "Convert Tabs to Spaces" feature
Prevention
Always configure your editor to insert spaces when you press Tab. This prevents the error from happening.