113

I have some existing code which isn't formatted consistently -- sometimes two spaces are used for indent, sometimes four, and so on. The code itself is correct and well-tested, but the formatting is awful.

Is there a place online where I can simply paste a snippet of Python code and have it be indented/formatted automatically for me? Alternatively, is there an X such that I can do something like X --input=*.py and have it overwrite each file with a formatted version?

John Feminella
  • 303,634
  • 46
  • 339
  • 357
  • 1
    [Here's how to get Sublime Text 2 to do it for you](http://stackoverflow.com/questions/14773271/how-to-fix-convert-space-indentation-in-sublime-text-2/14773274#14773274). – Magne Feb 08 '13 at 13:08
  • 1
    In vim: plugin [python-mode](https://github.com/klen/python-mode#-python-mode-python-in-vim) offers the command [`:PyLintAuto`](https://github.com/klen/python-mode#commands) using [autopep8](https://pypi.python.org/pypi/autopep8/) – Hotschke Aug 21 '13 at 11:44

7 Answers7

110

autopep8

autopep8 would auto-format your python script. not only the code indentation, but also other coding spacing styles. It makes your python script to conform PEP8 Style Guide.

pip install autopep8
autopep8 your_script.py    # dry-run, only print
autopep8 -i your_script.py # replace content

Update:

Many editors have pep8 plugins that automatically reformat your code right after you save the file. py-autopep8 in emacs

yapf

yapf is a new and better python code formatter. which tries to get the best formatting, not just to conform the guidelines. The usage is quite the same as autopep8.

pip install yapf
yapf your_script.py    # dry-run, only print
yapf -i your_script.py # replace content

For more information, like formatting configurations, please read the README.rst on yapf github


Update 2:

Black

Black is much better than yapf. It's smarter and fits most complex formatting cases.

d2207197
  • 1,284
  • 1
  • 9
  • 10
  • [ Webserver - ~/scripts ]$ autopep8 -i 2c.py [ Webserver - ~/scripts ]$ python 2c.py File "2c.py", line 9 from requests.adapters import HTTPAdapter ^ IndentationError: unexpected indent Not working for me ... – pkm Jun 16 '15 at 09:13
  • Python code blocks are defined by their indentation, so fixing the indentation is your job not autopep8. Tools can't and shouldn't change the level of indentation. – d2207197 Jul 04 '15 at 13:57
  • 3
    I disagree with black > yapf. Black is too inflexible in many cases. Many things can't be configured. I know that this is what the authors of black want to achieve, but I can't stand if someone is trying to make decisions in my projects. Especially when working in a **real** productive environment such as in companies, when programming is not just some kind of scientific game, you need to agree on a style with many persons and/or departments. And **this** is where black **horribly fails** with its "uncompromising" blabla. – JE_Muc Feb 17 '21 at 15:17
  • yapf doesn't fix tabs/spaces – don bright Mar 12 '23 at 03:09
58

Edit: Nowadays, I would recommend autopep8, since it not only corrects indentation problems but also (at your discretion) makes code conform to many other PEP8 guidelines.


Use reindent.py. It should come with the standard distribution of Python, though on Ubuntu you need to install the python2.6-examples package.

You can also find it on the web.

This script attempts to convert any python script to conform with the 4-space standard.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
23

Use black. It has deliberately only one option (line length) to ensure consistency across many projects. It enforces PEP8.

ndou
  • 1,048
  • 10
  • 15
  • 6
    You can also use repl.it to use `black` online (as the OP requested): https://repl.it/repls/BriefBetterSystemcall – damio Jun 21 '18 at 15:10
9

Found an offline solution with PyCharm

In PyCharm do:

1. Select all the Code:

[ctrl]+[A]

2. Format all the Code

[ctrl]+[alt]+[L]
mrk
  • 8,059
  • 3
  • 56
  • 78
2

Some editors have an auto-format feature that does this for you. Eclipse is one example (though you would probably have to install a python plug-in).

Have you checked whichever editor you use for such a feature?

Chris
  • 3,400
  • 1
  • 27
  • 41
  • +1 for eclipse autoformatting. Just open up the file and resave it in eclipse. It will fix the indentation, delete erroneous spaces between parenthesis and args, add spaces after commas in lists and args, add correct spacing around "=" signs, and all the other PEP 8 goodness. –  Apr 13 '10 at 08:14
  • 1
    No it won't. Eclipse formatting out-of-the-box does not do a thing with Python scripts, not even with PyDev installed as-is. – luis.espinal Feb 11 '15 at 19:02
  • 1
    Whoever neg-rep my comment, prove me wrong, or neg-rep me again, whatever rocks your boat. – luis.espinal Feb 18 '15 at 16:40
1

I am currently utilizing Ubuntu 18.04 LTS and VSC as a python IDE. My version of python is 3.10.5. I have noticed a difficulty in utilizing the python interpreter on my computer with the one used on bash. Reformatting has been one of those difficulties, although VSC does allow auto formatting it doesn't seem to work on my system. One workaround, using pip as a way to install separate python related libraries, is to install black using pip:

$ pip install black

after installing, simply utilize the command:

$ black <name of your python file.py>

This will directly alter and reformat the .py file and properly reformat your code.

0

Well you can simply use code editors/ interpreters like:-

  1. Visual Studio Code
  2. Sublime Text
  3. Pycharm(IDE)
  4. Spyder(IDE)

As per my experience Visual studio code has a great feature of auto formatting the code after you save it.