Autoindent Tool For Python In Mac

  1. Notepad++ Auto Indent Python
  2. Auto Indent Tool For Python In Mac And Cheese
Active9 months ago

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.

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. This is generally unnecessary. As the Extension will resolve the path to the formatter based on Python executable being used or configured in python.pythonPath of settings.json. If this cannot be found, then the formatter will be resolved based on the current environment Path settings.

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 FeminellaJohn Feminella
251k37 gold badges310 silver badges332 bronze badges

5 Answers

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.

Forunutbuunutbu
591k112 gold badges1286 silver badges1327 bronze badges

autopep8

Autoindent

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.

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.

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.

d2207197d2207197

Notepad++ Auto Indent Python

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

nbedounbedou

Found an offline solution with PyCharm

In PyCharm do:

Autoindent Tool For Python In Mac

1. Select all the Code:

Auto Indent Tool For Python In Mac And Cheese

2. Format all the Code

mrkmrk
2,8921 gold badge22 silver badges41 bronze badges

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?

ChrisChris
2,5691 gold badge15 silver badges32 bronze badges

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