python Python格式化工具

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1032393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 21:19:19  来源:igfitidea点击:

Python Formatter Tool

pythonformatter

提问by user127555

I was wondering if there exists a sort of Python beautifier like the gnu-indent command line tool for C code. Of course indentation is not the point in Python since it is programmer's responsibility but I wish to get my code written in a perfectly homogenous way, taking care particularly of having always identical blank space between operands or after and before separators and between blocks.

我想知道是否存在一种 Python 美化工具,例如用于 C 代码的 gnu-indent 命令行工具。当然,缩进不是 Python 中的重点,因为它是程序员的责任,但我希望以完全同质的方式编写我的代码,特别注意操作数之间或分隔符前后和块之间始终相同的空格。

回答by candide

I am the one who asks the question. In fact, the tool the closest to my needs seems to be PythonTidy(it's a Python program of course : Python is best served by himself ;) ).

我是问这个问题的人。事实上,最接近我需要的工具似乎是PythonTidy(当然,它是一个 Python 程序:Python 最好由他自己提供 ;))。

回答by tom

autopep8 attempts to automate making your code conform to pep8 coding standards

autopep8 尝试自动使您的代码符合 pep8 编码标准

https://pypi.python.org/pypi/autopep8

https://pypi.python.org/pypi/autopep8

回答by Eyal Levin

You can also try yapf:

您也可以尝试yapf

A formatter for Python files

Python 文件的格式化程序

https://github.com/google/yapf/

https://github.com/google/yapf/

回答by Vinko Vrsalovic

PyLinthas some formatting checks.

PyLint有一些格式检查。

回答by Andrew Hare

Have you looked at pindent?

你看过pindent吗?

回答by sgt pepper

blackis another recent option.

black是另一个最近的选择。

It provides deliberate and fast code formatting and is used by many projects such as pytest, SQLAlchemyand Pillowto name a few.

它提供了故意和快速的代码格式化,是由许多项目,如使用pytestSQLAlchemyPillow仅举几例。