Table Of Contents
In this post, we’ll provide 5+ fixes for the frustrating Indentation Error: unindent does not match any outer indentation level error.
One of the common errors in programming is the indentation error. It happens when unindent does not match any outer indentation level, but what is this error all about and how to fix it? Before we define what indentation is and why this error occurs, let us identify how programs work.
Each computer program has its own coding style guidelines. In programming, there is a set of standards used in writing the source code for a specific computer program. They are known as the code lay-out or the programming style. It helps the programmers read and understand a particular source code whose function is to prevent initiating errors.
And this error can really piss people off:
WHAT THE ACTUAL F IT SHOULD WORK BUT NOOOO
"compile error: unindent does not match any outer indentation level"
— solanaceae (@lc_potato) September 27, 2017
In programming, code lay-out is designed for a specific programming language.
Programming languages are letters taken from the alphabet that is formed in accordance with a particular set of rules. This is where indentation comes in. Programming languages use indentation to determine the limits of logical blocks of code. If codes are indented, codes are more eye-friendly. In other words, they will be readable. On the other hand, the indentation in Python is required to indicate which blocks of code a statement belongs to.
Indentation uses four spaces per indentation level. This is where indentation error usually takes place. This is where unindent does not match any outer indentation level. Mixing spaces with tabs is what causes the error.
Also, other factors that affect the alignment of the elements may cause the system to respond differently. There are guidelines, fixes, and preventive measures for this issue below.
But first- check out some more reaction from social media regarding this annoying error:
It also gives you a diagnostic error right when you run the code:
IndentationError: unindent does not match any outer indentation level.
— 🖥️🏳️🌈unsafe fn bot() ➡️ impl CRJ Shitposting (@KardOnIce) November 27, 2018
The most frustrating error for me in Python/ Spyder is "unindent does not match any outer indentation level". tab, space still not working….. I hate the error with passion #python #100DaysOfMLCode #ai #MachineLearning
— Oladeji Stephen (@RealSaintSteven) October 9, 2018
Understanding Python Indentation
The ICMEStudio YouTube channel shot a popular video clarifying how indentation works in Python:
How To Prevent The Indentation Error From Occuring?
The guidelines below are just the basic steps to follow in preventing indentation error occurs.
- First, it is important to align the codes with an opening delimiter. By using Python’s implicit line linking inside brackets, braces, and parentheses, these continuation lines should align wrapped elements vertically. A hanging indent can be used as well.
Note: In using a hanging indent, no arguments must be placed on the very first line. Also, to clearly differentiate the continuation line, an additional indentation must be used.
- Add four spaces to determine arguments from the rest. This will be considered as an extra level of indentation.
- Add an additional indentation level for hanging indents.
Note: Remember that the 4-space rule is nonmandatory for hanging indents.
How To Fix It?
The number of indentation always matters. Having a missing or extra space could cause an unexpected behavior that we consider the error. Within the similar block of code, statements should be intended at the same indentation level. So, here are preferred methods to fix the error.
For those who use Sublime Text as their source code editor, follow these steps:
- Set Sublime Text to use tabs for indentation.
- Go to view
- Choose indentation
- Convert Indentation to Tabs.
- Go to the sub-menu and look for the ‘Indent Using Spaces’ option and uncheck it.
This will automatically resolve the issue.
Note: Injecting copy-pasted code could be a source of the problem. So, avoid doing it. Space and tab problems can be prevented if you will display invisible characters within your text editor. It will quickly resolve indentation-related errors.
Also, you can avoid the issue by using the package Anaconda. Try following these steps:
- Install Anaconda.
- Open your sublime file.
- Look for ‘Open Spaces’ and right click.
- Choose Anaconda.
- Click on auto format.
- Click Done or Press CTRL+ALT+R
Video Guide: Sublime Text Line and Indentation Tools
For Python’s IDLE editor users, follow the steps below, if it suggests one similar message:
1) Choose All, e.g. Ctrl + A
2) Select Format -> Untabify Region
3) Double check if your indenting is still correct, then save and rerun the program.
This works well for Python 2.5.4 and Python 3.4.2
Note: Make sure not to mix space with the tab because Python is whitespace sensitive.
For Vim users, follow these steps and it will indent everything. It will also clear up any space you threw in.
- Hit escape.
- Type gg=G
For ATOM users, you can do these steps to resolve the error:
- Go to Packages.
- Choose Whitespace.
- Convert Spaces to Tabs.
- Check your file indentation:
python -m tabnanny yourFile.py
And here’s another way to resolve the issue. This method also works for ATOM users.
- Go to Packages.
- Choose Whitespace.
- Remove trailing whitespaces.
For indentation-related issues in Notepad++, you can fix the problem by doing this:
- For unexpected indentation:
- Go to the Search tab.
- Tap on replacing.
- Hit the radio button EXTENDED below.
- Replace \t with four spaces.
- For Outer indentation Level:
- Go to.
- Search tab.
- Tap on replacing.
- Hit the radio button EXTENDED below.
- Replace \n with nothing.
Video Guide: How To Indent HTML Tags In Notepad++
For Spyder users, look carefully at your ‘if statement’ and make sure that all if, elif and else statements have exactly the same spacing as if they are in the same line.
Use the reference below:
elif answer<=5 and answer>1:
print(“Welcome to the toddler’s club!”)
print(“No worries mate!”)
To understand more about IndentationError: unindent does not match any outer indentation level, we search through several Python message boards and support forums.
In general, users wanted information about IndentationError: unindent does not match any outer indentation level Vim/ ATOM/Jupyter/Notepad C++. They also wanted to know about how to handle IndentationError: unindent does not match any outer indentation level in Django and Pycharm.
A Python novice shared that he completed his code, and he was certain that he has programmed it correctly.
- However, he was getting an error that he didn’t know the meaning of.
- It said, “IndentationError: unindent does not match any outer indentation level.”
- So, he reached to the programming community for advice.
- They said that the most probable cause for such an error would be that he had mixed tabs with spaces which resulted in the wrong indentation.
- What they recommend was to search for tabs and then replaces the tabs with spaces.
Another user also ran into the same error. Following advice from other Python programmers, he looked at what tabs and spaces he had used. He discovered that his first line was spaced, but he had used tabs for the rest of the code. He was surprised by that because he didn’t know that tabs and spaces mattered in Python.
An individual remarked that it could be tricky to remember how to use tabs and space properly in Python. That’s why he recommended that you use the right text redactor to edit your Python code and mentioned that you could configure it to replace tabs with spaces so that you could avoid IndentationError: unindent does not match any outer indentation level errors.
— Python Questions (@Python_Quest) June 25, 2015
Another poster also mentioned that spaces are preferred in Python and that you can employ SublimeText because it shows spaces as dots. In this way, you can quickly find out where you’ve used tabs and fix it on the spot.
A poster explains that if you have used a tab somewhere instead of a space, you can change it quickly in Sublime. Simply go to View –> Indentation –> Convert Indentation to Tabs. He also recommends that you uncheck “Indent Using Spaces” in the same menu so that you don’t make an accidental mistake.
A Python user says that if you’re using ATOM, you also have the option to convert tabs to spaces when you get the indentation error. He explains that you must click on Packages > Whitespace > Convert Spaces to Tabs, and you’re ready to go.
Another forum member comments when you get IndentationError: unindent does not match any outer indentation level errors you can check for problems using “python -m tabnanny yourfile.py”. However, several users who have tried that solution said that they didn’t get any output and one poster complained that it showed an error in the wrong line.
my first python error…. IndentationError: unindent does not match any outer indentation level
— Hruhiko Soma (@harupiko) September 6, 2007
A Python expert explains that it’s true that spaces are preferred according to the Python style guide. However, as long as you use only spaces or only tabs you shouldn’t get indentation errors, but using both will cause a conflict.
Another user explains that besides mixing tabs and spaces in code, other sources of indentation problem will arise if you copy and paste code lines. He also suggests that you always display invisible characters like spaces/tabs in your editor so that you can spot a mismatch.
A forum commentator shares that his problem with IndentationError: unindent does not match any outer indentation level came from an incorrect configuration of PyDeck. That’s why he advises that you check the settings to make sure that they are the correct one. He observes that Python is very strict with how you should structure your code and that you should read the style guide again if you’re not sure which rules apply to Python.
Another poster states that you should rely on a good Python IDE if you’re serious about programming. He points about that an IDE can identify and highlight wrong indentations so that you don’t have to wonder what’s wrong with the code.
A beginner programmer said that he followed the advice of fellow Python users and that he replaced tabs with spaces. However, he still kept getting the same indentation error. It turned out that his indentation wasn’t consistent. He didn’t know that he should stick to four spaces per level of indentation, which is the recommended number.
Coding is a crucial and sensitive task. This is why programmers need to be extra careful in inserting program languages. In resolving issues like the indentation error, different approaches could be used to resolve the problem.
Also, there are things you need to know ahead. First, check the source code editor you are using because different procedure applies. Follow the correct settings and know the possible causes that may trigger the system to behave differently.
Also, you can rely on forums online and video tutorials since people around the world encounter the same issues as yours and might be able to share how they resolve it their way. Be knowledgeable and resourceful enough to gather more information as solutions may vary because of the system compatibility.
And lastly, don’t hesitate to share what works for you because it could also save someone on the other side.