Isort: Relative import sorting order

Created on 1 Apr 2016  路  4Comments  路  Source: PyCQA/isort

Is there a setting to change the sort order of relative imports from

from .. import x
from . import y

to

from . import y
from ..import x

so that one can comply with the Google style guide as per this linter https://github.com/public/flake8-import-order#configuration?

enhancement

Most helpful comment

Let me start by saying thank you for isort. It has made a positive impact on my personal and team's workflows!

Although the referenced flake8-import-order plugin may reject the descending order for relative imports, AFAICT the Google Python Style Guide makes no assertions about order of relative imports. In fact, it prohibits using relative imports at all:

Do not use relative names in imports. Even if the module is in the same package, use the full package name. This helps prevent unintentionally importing a package twice.

To be clear, by descending order, I mean:

from ...baz import x
from ..foo import y
from .bar import z

There are at least two arguments for isort to default to descending order for relative imports:

  1. The new ascending order is a regression from isort's previous behavior for imports of the form from ..module import name, which more common in my projects than the from .. import module form.
  2. The spirit of PEP-8 is to order imports from "least local" to "most local". Imports from ... are less local than those from .., and imports from . are most local. Descending relative imports achieves this ordering.

The original intent of this issue was to add an option to select the order of relative imports. I suggest that such an option remains a valid request for isort.

All 4 comments

Hi @lee-kagiso,

Thanks for bringing this to my attention! This is not something isort currently supports, but I'll work on adding support for this in the next release.

Thanks!

~Timothy

This is fixed as the default (no config necessary) and will be pushed into a new release today.

Thanks!

~Timothy

Let me start by saying thank you for isort. It has made a positive impact on my personal and team's workflows!

Although the referenced flake8-import-order plugin may reject the descending order for relative imports, AFAICT the Google Python Style Guide makes no assertions about order of relative imports. In fact, it prohibits using relative imports at all:

Do not use relative names in imports. Even if the module is in the same package, use the full package name. This helps prevent unintentionally importing a package twice.

To be clear, by descending order, I mean:

from ...baz import x
from ..foo import y
from .bar import z

There are at least two arguments for isort to default to descending order for relative imports:

  1. The new ascending order is a regression from isort's previous behavior for imports of the form from ..module import name, which more common in my projects than the from .. import module form.
  2. The spirit of PEP-8 is to order imports from "least local" to "most local". Imports from ... are less local than those from .., and imports from . are most local. Descending relative imports achieves this ordering.

The original intent of this issue was to add an option to select the order of relative imports. I suggest that such an option remains a valid request for isort.

I agree with @jpgrayson, with the recent release of isort it suddenly started reversing imports that were perfectly fine before.

Was this page helpful?
0 / 5 - 0 ratings