123

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?

2

7 Answers 7

120

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.

5
  • [ 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
    Commented Jun 16, 2015 at 9: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
    Commented Jul 4, 2015 at 13:57
  • 5
    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
    Commented Feb 17, 2021 at 15:17
  • yapf doesn't fix tabs/spaces
    – don bright
    Commented Mar 12, 2023 at 3:09
  • 1
    @JE_Muc things like rustfmt, gofmt, prettier show the opposite of the benefit of having a heavily opinionated formatter. The problem with flexibility is that developers almost always disagree on what they like. The more developers the worse it gets. This is eventually only resolved by letting go and letting a widely used formatter take the decisions and the blame. Otherwise its the loudest developer in the room that forces what they want which is worse.
    – icc97
    Commented Jun 20 at 9:04
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.

5
  • 4
    Perfect. Thanks! I used find . -type f -name "*.py" | xargs python reindent.py --nobackup. Commented Apr 12, 2010 at 21:26
  • 1
    Under Fedora you need to install the python-tools package to get reindent.py
    – John Slade
    Commented May 12, 2012 at 19:52
  • where it is located in Mavericks?
    – avi
    Commented Nov 21, 2013 at 5:48
  • @avi: Usually you can look up the answer here, but since Mavericks is too old, I'm guessing it would be in python2.7-examples.
    – unutbu
    Commented Jan 8, 2014 at 18:44
  • 1
    you should recommend black these days
    – user3064538
    Commented Aug 11, 2019 at 17:27
25

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

1
8

Found an offline solution with PyCharm

In PyCharm do:

1. Select all the Code:

[ctrl]+[A]

2. Format all the Code

[ctrl]+[alt]+[L]
3
  • 1
    What button is [strg]?
    – Stevoisiak
    Commented Mar 22, 2018 at 16:31
  • 1
    Its the german version of [ctrl]
    – PhilippB
    Commented May 13, 2022 at 16:53
  • Does PyCharm specify where it gets its formatting rules? Does it use something like Black or autopep8 in the background?
    – icc97
    Commented Jun 20 at 9:05
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?

3
  • +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.
    – user297250
    Commented Apr 13, 2010 at 8: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. Commented Feb 11, 2015 at 19:02
  • 1
    Whoever neg-rep my comment, prove me wrong, or neg-rep me again, whatever rocks your boat. Commented Feb 18, 2015 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.