Skip to content

IndentationError: unindent does not match any outer indentation level โ€‹

This error occurs when there is a mismatch in the indentation level of lines within the same block of code in Python. While Python allows different indentation levels for nested blocks, all lines belonging to the same logical block must be indented consistently. This error commonly happens when mixing tabs and spaces or using uneven spacing.

Here's an example of code that would trigger this error:

Code example that triggers IndentationError

python
def my_function():
    if True:
        print("Hello, world!")
     print("Goodbye, world!")

In the above example, the second print statement is indented with 3 spaces instead of 4, causing the indentation level to mismatch the outer block.

To fix this, you need to make sure that all lines of code within the same block are indented at the exact same level.

Code example with correct indentation

python
def my_function():
    if True:
        print("Hello, world!")
        print("Goodbye, world!")

This way, the second print statement is indented at the same level as the first one, and the error will be resolved.

Tip: Configure your code editor to insert spaces instead of tabs, and set a consistent indentation width (e.g., 4 spaces). This prevents accidental mixing of indentation characters, which is the most common cause of this error.

Dual-run preview โ€” compare with live Symfony routes.