How to fix Python indentation

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to fix Python indentation



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?





can you review this link and provide your inputs on how to use reindent.py module--stackoverflow.com/questions/12132481/installing-reindent-python/…
– user1050619
Aug 26 '12 at 18:05





As mentioned below by @andy-hayden look at this related question - basically autopep8 provides indentation and much much more: stackoverflow.com/questions/14328406/…
– Pierz
Dec 1 '14 at 9:41



autopep8





This is an insanely useful question, I find myself needing to do this quite often. It's not about "recommending a tool" so much as "how to do it".
– Andy Hayden
Feb 22 '16 at 6:20




12 Answers
12



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


reindent.py


Tools/scripts/



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.





Unfortunately, it's not part of the normal Python install on Debian and Ubuntu; it's split out into the python-examples package.
– ephemient
Jun 21 '09 at 20:03


python-examples





Excellent! So all that Debian and Ubuntu users need do is to apt-get that package. Alternatively, the script is also at svn.python.org/view/python/trunk/Tools/scripts/… .
– Alex Martelli
Jun 21 '09 at 20:52





@Shay Erlichmen: Try "python -tt yourscript.py"
– nosklo
Jun 22 '09 at 12:28





Fedora/RedHat/CentOS users should install the "python-tools" package.
– Cristian Ciupitu
Jun 30 '09 at 18:41





Just to add to ephemient's comment: once python-examples is installed, you'll find reindent in /usr/share/doc/pythonX.X/examples/Tools/scripts/reindent.py.
– Larry Hastings
Feb 28 '10 at 0:35


python-examples


/usr/share/doc/pythonX.X/examples/Tools/scripts/reindent.py



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


:h retab



For example, if you simply type



all your tabs will be expanded into spaces.



You may want to



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



If you're not using Vim,



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.


file.py


file.py.bak





Worked well. This is the easiest way to fix tab indentation mix-up .
– Srikant
Jun 26 '14 at 17:23





For sure not the easiest way.
– Pedro Lobito
Jul 26 '17 at 22:39



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).


--select=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.





this is awesome... I've been looking for a tool like ruby's rubocop for python
– OkezieE
Jan 21 '16 at 17:29





autopep8 fails if the syntax is incorrect. you can confirm that using python -tt <file.py>
– Nishant
Jan 16 at 10:34


autopep8


python -tt <file.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.


autopep8


PEP 8


nullstyle


nullcode


autopep8


nullformatting


pep8


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





i have just one line code with "import os" and 4 spaces before it but its not indenting that one ....any reason why
– pkm
Jun 16 '15 at 9:20






@pkm 4 spaces (or any indention that is a multiple of 4) is part of the PEP8
– rbaleksandar
Apr 7 '16 at 9:54





This is the simplest answer on this page.
– Aaron
Aug 29 at 21:22



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.





inconsistent spacing also
– Shay Erlichmen
Jun 21 '09 at 18:16





can you track down the original coder(s) and apply enhanced interrogation techniques? There is nothing worse than inconsistently indented code.
– Ben Hughes
Jun 21 '09 at 18:25





@Ben The inconsistent indention is the least of our problems from that guy :)
– Shay Erlichmen
Jun 22 '09 at 6:51



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.



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/



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.





Note that using the same file doesn't work and leaves you with an empty file, but this answer actually works and I use it when I need to convert my tabs to spaces. Could anyone explain the downvote? I'll up it +1.
– S. Kerdel
Jul 18 '16 at 14:34




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


sed -r 's/^([ ]*)([^ ])/112/' file.py





Illegal options -r.
– SmallChess
Oct 31 '17 at 7:46



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





python-mode is nice for writing Python, but nothing at the link describes how to fix indentation in an existing file.
– ephemient
Jun 21 '09 at 20:53





M-x untabify will turn tabs into space in emacs
– Paul Hildebrandt
Feb 14 '10 at 4:53





The link is (effectively) broken. It redirects to a page with the title "C Programming Language for Beginners " (!).
– Peter Mortensen
Aug 11 at 23:36




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





Type ":retab" in what context. In Vim?
– Peter Mortensen
Aug 11 at 23:40



:retab



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






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

React Native Navigation and navigating to another Screen problem