Over a million developers have joined DZone.
Platinum Partner

Help Teach Programming Disciplines with PythonTidy

· DevOps Zone

The DevOps Zone is brought to you in partnership with Librato.  Check out Librato's whitepaper on Selecting a Cloud Monitoring Solution.

PythonTidy is a utility to reformat Python source code according to Python PEP-8 style guide. It cleans up your source code and makes it adhere the community recommended practices.

PythonTidy does not lint or validate Python source code – it rewrites it to follow PEP-8 (and other) configured rules.

PythonTidy is very useful if you need to

  • clean up old codebases and make them to embrace PEP-8
  • work with people who do not yet follow strict programming disciplines (it helps to convert them if you can make them use a text editor which highlights the errors on background)

Adhering PEP-8 is also must when you are using a text editor which highlights linting errorsin your source code while typing. If your source code is full of unneeded style errors you’ll miss the actual linting errors among them.

PythonTidy was created by Chuck Rhode.

1. PythonTidy plug-in for Sublime Text 2

The basic PythonTidy works from the command line. However, in this blog I show how to use PythonTidy plugin, by  Andreas Zeidler, in Sublime Text 2 code editor with one key press integration. Sublime Text 2 has very powerful Python linting support throughSublimeLinterl package, highlighting style and linting errors when you type.

First install PythonTidy using git (note: Sublime Text 2 own package control installer does not work due to usage of Git submodules, you need to do this from command line).

Below is a screenshot with two style errors (no space after comma, bad number of newlines between module main level code blocks).

Now you simply press CONTRL+CMD+ALT+T: your source is fixed:

2. Configuring PythonTidy for Sublime Text 2

PythonTidy uses ~/.pythontidy.xml configuration file. A default setting value which most people probably want to change is the line length – PythonTidy shortens your line to 76 characters by default as recommended by PEP-8, and this is really quite short for the modern widescreens, wasting a lot of space. (I know there are some crazy people using tmux + vim + some other terminal stuff from golden early 80s and splitting their screen to 12 mini squares, but most of us don’t do that…)

Configuration options are explained in PythonTidyWrapper.py. More example settings are available in this Gist. Here is what I threw in the config:


  <parm name="COL_LIMIT" value="int(1000)" />

  <parm name="SHEBANG" value="" />
  <parm name="PARENTHESIZE_TUPLE_DISPLAY" value="bool(False)" />
  <parm name="ADD_BLANK_LINES_AROUND_COMMENTS" value="bool(False)" />
  <parm name="ADD_BLANK_LINE_AFTER_DOC_STRING" value="bool(False)" />
  <parm name="MAX_SEPS_FUNC_DEF" value="int(99)" />
  <parm name="MAX_SEPS_FUNC_REF" value="int(99)" />

  <parm name="WRAP_DOC_STRINGS" value="bool(False)" />
  <parm name="NORMALIZE_DOC_STRINGS" value="bool(False)" />

Note: Looks like you cannot add XML comments in config for now.

3. Other text editors

Please post as a comment if you know any other text editors with PythonTidy or background PEP-8′ing support. I am especially interest how one could intergrate this with Eclipse + PyDev.

Also more Sublime Text 2 tips for Python developers.


Published at DZone with permission of Mikko Ohtamaa , DZone MVB .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}