SyntaxPython Error

TabError

TabError: inconsistent use of tabs and spaces in indentation

Traceback

terminal
Traceback (most recent call last):
  File "main.py", line 3, in <module>
    print("mixed")  # spaces here but tab above
TabError: inconsistent use of tabs and spaces in indentation

What causes this error

The same indented block contains both tab characters and space characters. Python 3 does not allow mixing tabs and spaces within a single block.

How to fix it

Configure your editor to use spaces for indentation (4 spaces is standard). Use 'Find and Replace' to convert all tabs to spaces, or run `python -m tabnanny yourfile.py` to detect mixed indentation.

Code that causes this error

Broken
def greet():
	if True:
        print("mixed")  # spaces here but tab above

Fixed code

Fixed
def greet():
    if True:
        print("consistent")  # all spaces

About TabError

A TabError is raised when Python detects that a source file mixes tab and space characters for indentation in a way that is ambiguous. This is a subclass of IndentationError and therefore also a subclass of SyntaxError. Python 3 is stricter than Python 2 about mixing tabs and spaces — it will raise TabError whenever a single block contains both types of whitespace.

The problem is that tabs can represent different numbers of columns depending on the editor, so code that looks correctly aligned in one editor may be misaligned in another. The universal fix is to standardize on spaces — the Python community convention is four spaces per indent level. Most editors and IDEs have a setting to automatically convert tabs to spaces on save, and tools like autopep8 or black can reformat entire files.

Common scenarios

1

Writing code with missing colons, parentheses, or brackets

2

Mixing Python 2 and Python 3 syntax when upgrading a codebase

3

Copy-pasting code from the web with formatting issues or invisible characters

4

Using reserved keywords as variable or function names

Related errors