John Feminella
John Feminella

Reputation: 311605

How do I autoformat some Python code to be correctly formatted?

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?

Upvotes: 124

Views: 181865

Answers (8)

EsmaeelE
EsmaeelE

Reputation: 2698

Besides autoflake, pytlint and some other classic python linter/formatter you can follow recent hype on Rust and use extremely fast Ruff as a brand new fast python linter and formatter.

Install on Unix like OSes

curl -LsSf https://astral.sh/ruff/install.sh | sh

Run formatter for all files in current directory

ruff format

If you want to integrate ruff in IDE check below link.

https://astral.sh/blog/ruff-v0.4.5

Upvotes: 0

LaFrance Daniels III
LaFrance Daniels III

Reputation: 11

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.

Upvotes: 1

Moonwalker
Moonwalker

Reputation: 21

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.

Upvotes: 0

d2207197
d2207197

Reputation: 1384

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.

Upvotes: 120

ndou
ndou

Reputation: 1178

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

Upvotes: 26

mrk
mrk

Reputation: 10406

Found an offline solution with PyCharm

In PyCharm do:

1. Select all the Code:

[ctrl]+[A]

2. Format all the Code

[ctrl]+[alt]+[L]

Upvotes: 8

unutbu
unutbu

Reputation: 880289

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.

Upvotes: 58

Chris
Chris

Reputation: 3529

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?

Upvotes: 2

Related Questions