How to fix Python indentation

Use the reindent.py script that you find in the Tools/scripts/ directory of your Python installation:

Change Python (.py) files to use
4-space indents and no hard tab
characters. Also trim excess spaces
and tabs from ends of lines, and
remove empty lines at the end of
files. Also ensure the last line ends
with a newline.

Have a look at that script for detailed usage instructions.


NOTE: If your linux distro does not have reindent installed by default with Python:

Many linux distros do not have reindent installed by default with python –> one easy way to get reindent is to do pip install reindent.

p.s. An alternative to pip is to use your distros package manager (i.e. apt-get, yum, dnf) but then you need to figure out what package has the command line tool because each distro has the tool in a different package.

Leave a Comment