Python Forum
Fixing indentation issues. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Fixing indentation issues. (/thread-15791.html)



Fixing indentation issues. - MuntyScruntfundle - Jan-31-2019

Hi there.

I know this is an editor problem, not Python itself, but it's driving me nuts, it have have cropped up with other people and I'm interested in what they did.

I have built up a pretty big collection of python scripts now and I have a need to edit them in different locations. The code is centralised, but I maybe on a windows machine one day, a MacBook the then, or vnc'd to a unix box. I always prefer a decent editor with some intellisense, so where possible I use VS Code, if that's not available I'll use nano.

However, after making changes and saving scripts they may then not open correctly in other apps, indentation is messed up. It may be one line which I can delete back and put back in place, it may be some invisible character that I have to chase all the way up or down my code, or it may be a stupid indentation that I just cannot fix, usually a double indentation after a def that isn't correct, but the code won't run without it.

To compound the problem if I go back to the previous editor to see what's gone wrong the code is either worse or will give random incorrect runtime errors. Of course to the compiler the errors are valid, but they're not to the eye.

The problem is this makes bug fixing a complete nightmare. Imagine trying to fix a variable that isn't getting set in a 300 line script where the indentation is all incorrect.

I have in the past had to completely re-write scripts identically to the eye to get them working again. This is a ball ache.

I do realise with Python and it's fairly loose structure, no end if, no end sub, no end function, it' damn difficult to fix indentation errors if you're not intimate with the code. I WISH there was an option to force strict ends. But, is there an app out there anywhere that can fix these odd problems? It must be a character out of place, a tab that can't be seen, a space, some other cr/lf issue.

I know the bet practice would be - Only use one editor in one location, but this just isn't possible. And in this day and age it should be possible to hand your code to someone else and receive back changes without this crap.

Any other suggestions?

Many thanks.


RE: Fixing indentation issues. - ichabod801 - Jan-31-2019

First, I would make sure all of my editors are set to the same indentation settings. Four spaces, tabs, whatever. Just pick one and make sure all of the editors are set the same.

Second, the way to fix this is to get an editor that shows you the invisible characters. Then go through the code looking for discrepancies. Use find as well. If you are using tabs, search for double spaces or return and space. If you are using spaces, search for tabs.

Third, debug this. Think up a bunch of short functions, each using a loop or a conditional. Then write them to the same file, using a different editor for each function. Then, go back to an editor that can show the invisible characters, and look for discrepancies.


RE: Fixing indentation issues. - Larz60+ - Jan-31-2019

additional: many IDE's include auto formatting commands which work well, VSCode included
on windows: Shift Alt F
on Mac Shift Option F
On Linux: Ctrl Shift I


RE: Fixing indentation issues. - MuntyScruntfundle - Feb-02-2019

Thanks folks, I will follow these steps and go through everything. I suspect it also has something to do with the python interpreters loaded into vs code, but I may be wrong.

Thanks again.


RE: Fixing indentation issues. - MuntyScruntfundle - Feb-02-2019

There's definitely an extension issue.

On the MacBook half of my code is squiggly green with the warning "Spaces and tabs, expected spaces" although it's set to use tabs, not spaces. And on Windows with all the same editor settings the code is accepted as perfect.

I've been through the code with a nit comb VS Code with "Show all whitespace", notepad++ with "Show all characters", can't find a single thing wrong.

What's most confusing is looking at the code on the MacBook, why is half of my code ok and half of it not? There are NO differences in indents, tabs, spaces, coding style, etc. I've been over and over it. There's something around half way through where the extension is resetting a flag or something stupidly small.

Where do you post support requests for VS Code? I'm happy to work with them to get this hammered out.

Thanks again.


RE: Fixing indentation issues. - wavic - Feb-02-2019

It's recommended to use spaces instead of tabs so any editor could get it right. In any IDE editor, you can set the tabs to insert four spaces. So it is not needed to press the space button repeatedly but the tab.


RE: Fixing indentation issues. - metulburr - Feb-02-2019

i had a similar problem when i first started python. It was a nightmare. I used tabs at first, and someone here got me to use spaces. Using spaces guarantees a level of consistency that tabs do not.

In Geany IDE, there are options for converting the current file to X type of space as shown in the image. In this way i dont have to search out for spaces or tabs, i just convert the file and done


RE: Fixing indentation issues. - wavic - Feb-02-2019

Hah! Didn't know about it.
Anyway, one could write a python script to replace all tabs with four spaces. It's a pretty simple exercise.


RE: Fixing indentation issues. - ichabod801 - Feb-02-2019

Tabs or spaces shouldn't matter. Any text editor that can't handle tabs doesn't deserve to be called a text editor.


RE: Fixing indentation issues. - snippsat - Feb-02-2019

VS Code fix indentation with Right click --> Format Document.
I have formater set to use Black(An auto-formatter that really is good).
Look here how to setup.