Skip to content

TabError: inconsistent use of tabs and spaces

Quick summary

Your file uses both tabs and spaces for indentation within the same block — Python cannot determine the indentation level and raises TabError.

Why this happens

  • Some lines use tabs and others use spaces for indentation in the same function or block

  • Your editor inserted a tab character when you expected it to insert spaces (or vice versa)

  • Code was pasted from different sources with different indentation styles into the same file

Minimal example

✗ Broken code
def f():
	if True:
	    print('mixed')  # tab then spaces = TabError
✓ Fixed code
def f():
    if True:
        print('aligned')  # all spaces, 4 each

Using only spaces (4 per level) for all indentation eliminates the ambiguity that causes TabError.

How to diagnose

  • Run python -tt yourfile.py to detect tab/space mixing as an error with line numbers

  • Use cat -A or expand -t 4 to visualize tab characters in the file and find mixed indentation

  • Configure your editor to show whitespace and identify which lines use tabs vs spaces

How to fix

  • Convert all tabs to spaces using expand -t 4 yourfile.py or your editor's tab-to-spaces feature

  • Configure your editor to insert spaces when pressing Tab and set the tab width to 4

  • Re-indent the problematic block consistently with either all tabs or all spaces (spaces recommended)

How to prevent

  • Set your editor to use spaces for indentation and never insert literal tab characters

  • Add a .editorconfig file with indent_style = space and indent_size = 4 to enforce consistent indentation

Related Resources

Related Glossary

Related Lessons

← Back to language