Python borked indentation
I saw that there are similar names. But my case looks a little weirder. I've used a mixture of PyCharm and Vim somehow (and inside Vim I have tabstop = 4 and shiftwidth = 2) and my Python code seems to be un-fixabl-y borked, indented. I saw for the first time that everything was wrong in Wim, so I aligned everything again; but then when I run it I get an error that there is unexpected indentation even though everything looks perfectly consistent in Vim. Here's an example (what it looks like in Vim):
for f in files: for line in f: items = line.strip().split() items = items[2:] items = ' '.join(items).split(', ')
When I run it I get:
File "getEsSynonymLSAVectors.py", line 136 items = items[2:] ^ IndentationError: unexpected indent
I've used PythonTidy , I've used
, I've tried
, I've tried manual re-alignment - nothing seems to fix it. Any experiences / advice would be appreciated.
source to share
Python treated a tab as 8 spaces by default, if you get indentation you usually want to switch tabs to spaces (or vice versa, but I usually find spaces easier to deal with). So make sure vim displays the tab as 8 spaces (
) to see what python sees.
To fix tab errors in vim, I usually do the following: I need to see the tabs first, so I turned on the search highlighting (
) and did a tab search (
). Then I go through the areas that need to be canceled. Then I'll try to find the correct vim bookmark width setting for the file (
until everything looks good), enable the expand tab (
), then run the auto-commit of the tabs (
), When all else fails, reload manually.
If you are using a version control tool, make sure you have files scattered before the changes, and manually check that you have not entered an error due to unintentional indentation level changes. If you are not using version control, save a backup and run diff on the files.
source to share