Sublime Text 3 – sporadic indentation errors on Python files

pythonsublime-text-3

Sublime Text, build 3175, Macos

Every so often I will randomly get an IndentationError: unexpected indent message on a previously working Python file.

Inspecting the file in the editor does not show an indentation issue and I suspect a tab got sneaked in somehow.

Clicking at the bottom right of the window, Spaces, next to Python which indicates the source language, allows me to pick Convert indentation to spaces. That "changes the file" which now shows as changed and will request a save if closed. And my problem is solved.

This has only happened since the past 2 or 3 months and I never had this problem before, so I assume it was a recent change in Sublime that occasionally mangles either the tab vs. space indentation.

This is extremely irritating as there is a) no indication of what caused the problem and b) it can happy at any time on any file that has been edited at some point. And even more so when it hits at the end of a long build process.

How do I avoid this? I tried creating a Packages/User/Python.sublime-settings file, with the following in it, but that didn't help.

// These settings override both User and Default settings for the Python syntax
{
    "translate_tabs_to_spaces" : 1
}

Best Answer

As far as I know, the translate_tabs_to_spaces only accepts boolean values, so you need to write your settings like this:

{
    "translate_tabs_to_spaces": true
}

I also recommend setting draw_white_space to all. This highlights all whitespace, making it easier to distinguish spaces from tabs:

enter image description here

Related Question