IndentationError: unexpected indent
TL;DRThis Python error happens when a line of code starts with the wrong amount of spaces or tabs compared to the surrounding code, breaking the required structure.
Most likely cause: Mixing tabs and spaces for indentation in the same file or block
Try first: Open the file in a text editor and check the reported line
Likely causes
- Mixing tabs and spaces for indentation in the same file or blockmost likely
- An extra or misplaced indent on a line that should align with otherspossible
- Copy-pasting code that brought inconsistent spacing from another sourceless likely
How to fix it
Safest first: SafeChanges settingsDangerous
Open the file in a text editor and check the reported lineSafe
Look at the exact line number shown in the full error message and count the leading spaces or tabs to ensure they match the block above it.
Convert all indentation to spaces onlyChanges settings
Select the entire code and replace any tab characters with the equivalent number of spaces (usually 4 per level).
If that didn't fix it
Paste the full error message including the line number and a few lines of surrounding code into an online Python syntax checker.
When to see a technician
If the code is part of a larger application or you cannot locate the mismatched indent after checking the file, ask an experienced Python developer to review it.
Quick answers
- What does IndentationError: unexpected indent mean?
- This Python error happens when a line of code starts with the wrong amount of spaces or tabs compared to the surrounding code, breaking the required structure.
- What causes IndentationError: unexpected indent?
- Mixing tabs and spaces for indentation in the same file or block. (most likely) An extra or misplaced indent on a line that should align with others. (possible) Copy-pasting code that brought inconsistent spacing from another source. (less likely)
- How do I fix IndentationError: unexpected indent?
- Try these in order, safest first: 1. Open the file in a text editor and check the reported line. 2. Convert all indentation to spaces only.
- What if that didn't fix it?
- Paste the full error message including the line number and a few lines of surrounding code into an online Python syntax checker.
- When should I see a technician?
- If the code is part of a larger application or you cannot locate the mismatched indent after checking the file, ask an experienced Python developer to review it.
Related errors
codeUncaught SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSONcodeERROR: Could not find a version that satisfies the requirement (from versions: none)codeTypeError: can only concatenate str (not "int") to strcodeerror: Your local changes to the following files would be overwritten by merge: Please commit your changes or stash themcodefatal: Authentication failed for 'https://github.com/user/repo.git/'codeModuleNotFoundError: No module named requests