5

I am looking for a plugin for Vim, to organise Python import statements according to PEP 8:

Imports should be grouped in the following order:

  • standard library imports
  • related third party imports
  • local application/library specific imports

You should put a blank line between each group of imports.

Furthermore I would like it to alphabetically sort the imports, within each section.

  • Must: Split standard library imports from all others
  • Would: Split imports that have been installed (3rd party) from local imports
  • Would: Alphabetically sort imports within that section
  • Must: work for Python 2.7
  • Would work for all version of Python
  • Ideally would be installable as a .vimball

If it has some kind of weird dependency, then it must work under Linux, but most Vim plugins work on anything Vim does.

5

I don't know if there's a better plugin to work specifically with imports, but a good bet would be to use the Python-mode (https://github.com/klen/python-mode) plugin.

It does a lot of things related to coding in python (better syntax highlight, code checking, Pydoc integration, refactoring etc.).

It can also auto add and organize imports according to PEP8, using the python-rope library.

To answer your more specific points:

  • It does split standard library imports from others;
  • It does not split between 3rd party and local imports;
  • It does sort imports alphabetically inside a section, and lists import foo before from foo import bar statements;
  • Works for Python 2.6+ and 3.2+;
  • I couldn't find a vimball for it, but you can install it:

    • From a zip (available at http://www.vim.org/scripts/script.php?script_id=3770)
    • From source
    • Using your favourite plugin manager (mine is Vundle (gmarik/Vundle.vim in github))
    • With APT, if using a Debian-based linux distro
    • (You can find installation links and more detailed instructions in the repo README)
  • The only "weird dependency" needed to use the plugin is that your Vim installation must have been compiled with python support (which is usually true, AFAIK)
  • 1
    Welcome to SoftwareRec.SE. This is a fantastic first answer. Well done. – Lyndon White Aug 15 '14 at 0:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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