PEP8 suggests that:
Imports should be grouped in the following order:
- standard library imports
- related third party imports
Have a look at https://pypi.python.org/pypi/isort or https://github.com/timothycrosley/isort
isort parses specified files for global level import lines (imports outside of try / excepts blocks, functions, etc..) and puts them all at the top of the file grouped together by the type of import:
- Future
- Python Standard Library
- Third Party
- Current Python Project
- Explicitly Local (. before import, as in: from . import x)
Custom Separate Sections (Defined by forced_separate list in configuration file) Inside of each section the imports are sorted alphabetically. isort automatically removes duplicate python imports, and wraps long from imports to the specified line length (defaults to 80).
https://pypi.python.org/pypi/flake8-isort plugs this functionality into flake8