Unexpected Indentation in Visual Studio Code with Python

Viewed 14263

the following code

accounts = f.get_sepa_accounts()

for account in accounts:
    print(account)

is throwing the following error:

print(account)   File "<stdin>", line 1
print(account)
^ IndentationError: unexpected indent

I just moved to Visual Studio Code, so no idea if this is a problem specific to the application. I tried 4 spaces and a tab already..Doesn't do anything.

Thanks!

3 Answers

You have probably used tabs when indenting. You can check this by selecting the code. When selected the tabs are highligted with a right pointing arrow in VS code as shown in the image below:-

enter image description here

Notice the two arrows in the second line. See how the second line is error because of this. You fix this by selecting the two tabs and pressing ctrl +h. Replace with the right amount of spaces.

In short replace tabs with spaces.

vscode solution

Press F1 and start typing 'Indentation to spaces' and you will be able to find 'Convert Indentation to Spaces' option, select and click enter.

Next time, mostly you just have to press F1 and enter because it is selected by default since you used it recently.

F1 and enter

Restart Visual Studio Code.

Sometimes this can help.

Also ensure that you choose the right linter (CTRL + SHIFT + P > "Python: Select Linter" and choose f.e. pylint) before you restart.

Related