How to Fix Python TabError: Inconsistent Use of Tabs and Spaces
In this tutorial, you'll learn about How to Fix Python TabError: Inconsistent Use of Tabs and Spaces. We cover key concepts, practical examples, and best practices.
The Problem
Python raises TabError: inconsistent use of tabs and spaces in indentation when a file mixes tab characters and spaces for indentation. Python interprets tabs and spaces differently and refuses to guess the intended indentation level.
Quick Fix
Step 1: Identify the file and line
Run Python with the -t flag to list all tab inconsistency warnings:
python3 -t script.py
script.py:10: TabError: inconsistent use of tabs and spaces in indentation
Step 2: Convert tabs to spaces with expand
Use expand to replace all tabs with spaces:
expand -t 4 script.py > fixed_script.py
mv fixed_script.py script.py
This replaces each tab with four spaces, matching Python's convention.
Step 3: Auto-format with black
Install and run black to fix the entire file automatically:
pip install black
black script.py
reformatted script.py
All done! 1 file reformatted.
Step 4: Auto-format with autopep8
Alternatively use autopep8 which also fixes indentation:
pip install autopep8
autopep8 --in-place --aggressive script.py
Step 5: Configure your editor to use spaces
Make VS Code convert tabs to spaces on save:
{
"editor.insertSpaces": true,
"editor.tabSize": 4,
"files.insertFinalNewline": true
}
Alternative Solutions
Configure your editor to show invisible characters
In VS Code, enable rendering of whitespace to see tabs vs spaces:
{
"editor.renderWhitespace": "all"
}
Use a pre-commit hook to catch tab errors
Add a pre-commit hook that checks for tab characters:
echo '#!/bin/sh' > .git/hooks/pre-commit
echo 'python3 -t -m py_compile "$@"' >> .git/hooks/pre-commit
Common Mistakes to Avoid
Using the Tab key without checking editor settings. Most editors default to tabs. Configure your editor to insert spaces before writing Python code.
Copying code from web pages that use tabs. Code snippets from forums or blogs may contain tabs mixed with spaces. Paste through a linter or use expand.
Assuming a file is fine because it runs on your machine. A file with mixed tabs and spaces may work in one editor but fail in another. Use python3 -t in CI.
Pro Tips
Install an editor plugin that shows whitespace. VS Code's "indent-rainbow" extension colors indentation levels so tab/space mixtures are obvious.
Use pre-commit hooks for linting. Add pre-commit to your project with a hook that runs black and checks for tab characters before each commit.
Run flake8 with tab checking. flake8 reports tab-related issues: pip install flake8 && flake8 --select=W191 script.py.
Use flake8 to detect tab errors in CI. Add flake8 with the W191 rule to your CI pipeline to catch tab and space mixing before code is merged.
Prevention
- Configure your editor to insert spaces when Tab is pressed.
- Use
python3 -t script.pyin CI to catch tab errors before merging. - Add a
.editorconfigfile withindent_style = spaceandindent_size = 4.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro