How to fix Python indentation

How to fix Python indentation

Use the 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.

I would reach for autopep8 to do this:

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

$ # make these changes
$ autopep8 path/to/ --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.

How to fix Python indentation

If youre 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 (thats 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


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 youre not using Vim,

perl -i.bak -pe s/t/ x(8-pos()%8)/eg

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

Leave a Reply

Your email address will not be published. Required fields are marked *