How to fix Python indentation

Python

Python Problem Overview


I have some Python code that have inconsistent indentation. There is a lot of mixture of tabs and spaces to make the matter even worse, and even space indentation is not preserved.

The code works as expected, but it's difficult to maintain.

How can I fix the indentation (like HTML Tidy, but for Python) without breaking the code?

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

I would reach for autopep8 to do this:

$ # see what changes it would make
$ autopep8 path/to/file.py --select=E101,E121 --diff

$ # make these changes
$ autopep8 path/to/file.py --select=E101,E121 --in-place

Note: E101 and E121 are pep8 indentation (I think you can simply pass --select=E1 to fix all indentation related issues - those starting with E1).

You can apply this to your entire project using recursive flag:

$ autopep8 package_dir --recursive --select=E101,E121 --in-place

See also Tool to convert Python code to be PEP8 compliant.

Solution 3 - Python

If you're using Vim, see :h retab.

                                                        :ret :retab
:[range]ret[ab][!] [new_tabstop]
Replace all sequences of white-space containing a
<Tab> with new strings of white-space using the new
tabstop value given.  If you do not specify a new
tabstop size or it is zero, Vim uses the current value
of 'tabstop'.
The current value of 'tabstop' is always used to
compute the width of existing tabs.
With !, Vim also replaces strings of only normal
spaces with tabs where appropriate.
With 'expandtab' on, Vim replaces all tabs with the
appropriate number of spaces.
This command sets 'tabstop' to the new value given,
and if performed on the whole file, which is default,
should not make any visible change.
Careful: This command modifies any <Tab> characters
inside of strings in a C program.  Use "\t" to avoid
this (that's a good habit anyway).
":retab!" may also change a sequence of spaces by
<Tab> characters, which can mess up a printf().
{not in Vi}
Not available when |+ex_extra| feature was disabled at
compile time.

For example, if you simply type

:ret

all your tabs will be expanded into spaces.

You may want to

:se et  " shorthand for :set expandtab

to make sure that any new lines will not use literal tabs.


If you're not using Vim,

perl -i.bak -pe "s/\t/' 'x(8-pos()%8)/eg" file.py

will replace tabs with spaces, assuming tab stops every 8 characters, in file.py (with the original going to file.py.bak, just in case). Replace the 8s with 4s if your tab stops are every 4 spaces instead.

Solution 4 - Python

autopep8 -i script.py

Use autopep8

autopep8 automagically formats Python code to conform to the PEP 8 nullstyle guide. It uses the pep8 utility to determine what parts of the nullcode needs to be formatted. autopep8 is capable of fixing most of the nullformatting issues that can be reported by pep8.

pip install autopep8
autopep8 script.py    # print only
autopep8 -i script.py # write file

Solution 5 - Python

Using Vim, it shouldn't be more involved than hitting Esc, and then typing...

:%s/\t/    /g

...on the file you want to change. That will convert all tabs to four spaces. If you have inconsistent spacing as well, then that will be more difficult.

Solution 6 - Python

There is also PythonTidy (since you said you like HTML Tidy).

It can do a lot more than just clean up tabs though. If you like that type of thing, it's worth a look.

Solution 7 - Python

On most UNIX-like systems, you can also run:

expand -t4 oldfilename.py > newfilename.py

from the command line, changing the number if you want to replace tabs with a number of spaces other than 4. You can easily write a shell script to do this with a bunch of files at once, retaining the original file names.

Solution 8 - Python

The reindent script did not work for me, due to some missing module. Anyway, I found this sed command which does the job perfect for me:

sed -r 's/^([  ]*)([^ ])/\1\1\2/' file.py

Solution 9 - Python

If you're lazy (like me), you can also download a trial version of Wingware Python IDE, which has an auto-fix tool for messed up indentation. It works pretty well. http://www.wingware.com/

Solution 10 - Python

Try Emacs. It has good support for indentation needed in Python. Please check this link http://python.about.com/b/2007/09/24/emacs-tips-for-python-programmers.htm

Solution 11 - Python

Try IDLE, and use Alt + X to find indentation.

Solution 12 - Python

I have a simple solution for this problem. You can first type ":retab" and then ":retab!", then everything would be fine

Solution 13 - Python

In case of trying to find tool to make your 2-space indented python script to a tab indented version, just use this online tool:

https://www.tutorialspoint.com/online_python_formatter.htm

Solution 14 - Python

There is also a Google awesome project called YAPF

It can automatically reformat the whole project or check if the project has correct indentation/format. I used that in a commercial project and I recommend that.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShay ErlichmenView Question on Stackoverflow
Solution 1 - PythonAlex MartelliView Answer on Stackoverflow
Solution 2 - PythonAndy HaydenView Answer on Stackoverflow
Solution 3 - PythonephemientView Answer on Stackoverflow
Solution 4 - PythonPedro LobitoView Answer on Stackoverflow
Solution 5 - PythonBen HughesView Answer on Stackoverflow
Solution 6 - PythonPaul HildebrandtView Answer on Stackoverflow
Solution 7 - PythonCrowmanView Answer on Stackoverflow
Solution 8 - PythonMartin VegterView Answer on Stackoverflow
Solution 9 - PythonYanskyView Answer on Stackoverflow
Solution 10 - PythonArnkrishnView Answer on Stackoverflow
Solution 11 - PythonanshumanView Answer on Stackoverflow
Solution 12 - PythonhanqiangView Answer on Stackoverflow
Solution 13 - PythonMehrdad SalimiView Answer on Stackoverflow
Solution 14 - PythonKamil KuczajView Answer on Stackoverflow