Visual Studio Code Python Indentation Issues

7,848

There are some Text Editor settings which control the indentation, just check File > Preferences > Settings and search for "Indent"

Intend settings part 1 Intend settings part 2

Plus in the bottom right corner you can see and change the setting for the document currently working on, just click on it

Intend settings per document

I assume this is wrong and shows something different than 4. If you are working mostly in python, perhaps it would make sense to set the default to 4 in the general settings?

Share:
7,848

Related videos on Youtube

zmirina
Author by

zmirina

Updated on September 18, 2022

Comments

  • zmirina
    zmirina over 1 year

    So I have been having issues with my VSC and python 3.7 It works when I try to run the code through Sublime and terminal, and seems to fix itself after I restart visual studio, but always returns within 10 minutes or so.

    When i write code in python, automatic indents are not correct. The program as well as compiler, when I try to run it in terminal, complain about it. (As seen in picture 1) However, even when I try to manually change it to the correct position, compiler and VSC complain and do not let me compile it because of unexpected indentation. (As seen in picture 2)

    I have reinstalled VSC a few times because of this issue, I have freshly reinstalled macOS Mojave, and I have reinstalled python 3.7 and extensions for VSC. The problem resolves itself for a little bit if I restart the program, but I would rather not have to restart the program every other time I need to indent things in python (where indentation is rather critical)

    When my brother, who is a windows user, copies my code to his VSC, it does not show the same issue, so I'm completely lost as to what to do.

    how VSC indents the code automatically

    how VSC behaves when i fix indentation manually

    • Anaksunaman
      Anaksunaman over 4 years
      As an FYI, your editor is complaining about inconsistent use of tabs and spaces. Python cannot have mixed tabs/spaces for indentations. And you can clearly see in your first screenshot that every line begins with four spaces (a common default if there is no tab \t character present) while your else block has five spaces (the default number of spaces when a tab \t character is present). So you probably want to check the settings for Editor:Insert Spaces (as noted in the current given answer) is marked (so you get ex. four spaces rather than a tab \t character when you press Tab).
    • Anaksunaman
      Anaksunaman over 4 years
      And to be clear, adding additional spaces with an existing tab \t characters in the line won't fix the issue. You have to delete any "blank" characters before the start of the code and insert the proper number of spaces (or whatever) yourself. Moreover, you can only use tabs or spaces for indentations in the entire script. Regarding importing code, I am guessing your brother likely has an option to convert tabs to spaces automatically set, which would eliminate this problem when transferring the code itself.
  • papanito
    papanito over 4 years
    Sorry no native english here :-) However I updated the last sentence to make it more clear
  • Anaksunaman
    Anaksunaman over 4 years
    Much clearer now. Thanks! =)