Is there an online or offline utility that will format/pretty-print Python source code?
http://pypi.python.org/pypi/PythonTidy is an excellent, simple script.
I've found that PyLint and other code analysis tools all choke on pyrex, twisted and other modules.
If you want formatting, just use PythonTidy.
Depends of what you mean by "format/pretty-print Python source code".
There's Pygments that do syntax highlighting and you can try it online at Lodgeit (it's a pastebin).
There's also the pprint module in the standard library that pretty-print python data sctructures (dict, list, etc.).
If you need help to format you own python code you can use some text editors that have tools to do that, like PyDev/Eclipse.
http://www.polystyle.com/features/python-formatter.jsp
Polyglot, commercial (cheap) source code beautifier/obfuscator for several languages, including Python. Also: website has not been updated in 5 years, runs on Windows, so it may not be what you need.
A better alternative would be autopep8:
autopep8 automatically formats Python code to conform to the PEP 8 style guide. It uses the pep8 utility to determine what parts of the code needs to be formatted. autopep8 is capable of fixing most of the formatting issues that can be reported by pep8.
Since I found the source of PythonTidy to be somewhat messy and hard to extend, I have recently started a new and fully open source project for Python formatting. It uses the tokenize
library in Python to make sure we tokenize and untokenize correctly and is build to be very extendable.
Please take a look and let me know if you have any issues and/or have feature requests: https://github.com/WoLpH/python-formatter
- I aim for 100% test coverage using Coveralls as a check: https://coveralls.io/r/WoLpH/python-formatter
- And Travis for automatic testing: https://travis-ci.org/WoLpH/python-formatter
- Documentation can be found on Read The Docs: https://python-formatter.readthedocs.org/en/latest/
The programs ...
- Pygments (Outputs HTML/LaTex/ANSI/RTF)
- source-highlight (Outputs HTML/LaTex/DocBook/Texinfo/ANSI/ODF)
- GNU Enscript (Outputs HTML/PDF/RTF)
... can all pretty print Python source code with color. (They can also pretty print other languages.)
They have Ubuntu package names pygments
, source-highlight
, enscript
, respectively.
I found python beautifier from this link http://arachnoid.com/python/pybeautify_program.html
It's quite helpful for me.
来源:https://stackoverflow.com/questions/2913324/python-source-formatter-pretty-printer