Isort: Need clarification on submodule import sort order

Created on 13 Sep 2020  路  4Comments  路  Source: PyCQA/isort

I have the following imports in this order

from pack1.pack2 import mod1
from pack1.pack2.mod2 import Class1
from pack1.pack2.mod2 import Class2

isort sorts the imports as given below

from pack1.pack2.mod2 import Class1
from pack1.pack2.mod2 import Class2
from pack1.pack2 import mod1

I was expecting it in the below given order

from pack1.pack2 import mod1
from pack1.pack2.mod2 import Class1
from pack1.pack2.mod2 import Class2

I want to know whether its intended and any way to get it to my expectation.
If its intended, is there any documentation available?

The isort config used is

# configs details can be looked at
# - https://pycqa.github.io/isort/docs/configuration/options/
[settings]
profile=google
line_length=80
skip_gitignore=True
honor_noqa=True
float_to_top=False
combine_as_imports=True
from_first=False

Most helpful comment

The google profile defaults to using lexicographical sorting, which will produce this output. You can turn it off by setting lexicographical to False.

All 4 comments

can you verify which version of isort who are using?

For the most recent release i.e 5.5.2, for the given input I get the following output for the given configuration:

from pack1.pack2 import mod1
from pack1.pack2.mod2 import Class1
from pack1.pack2.mod2 import Class2

I was using 5.4.2 version. I even tried with the version 5.5.2. But the above given example is wrong i guess
@anirudnits can you try this?

from pack1.pack2 import mod1
from pack1.pack2.kod2 import Class1
from pack1.pack2.kod3 import Class2

This gets sorted as

+from pack1.pack2.kod2 import Class1
+from pack1.pack2.kod3 import Class2
+from pack1.pack2 import mod1

I was expecting like this

+from pack1.pack2 import mod1
+from pack1.pack2.kod2 import Class1
+from pack1.pack2.kod3 import Class2

Is my expectation wrong?

The google profile defaults to using lexicographical sorting, which will produce this output. You can turn it off by setting lexicographical to False.

Thanks @timothycrosley , @anirudnits. That solves the issue . Closing it

Was this page helpful?
0 / 5 - 0 ratings