Isort: Sort statements by package name, ignore ‘import’ vs ‘from … import’

Created on 6 Nov 2017  ·  7Comments  ·  Source: PyCQA/isort

A sensible ordering for import statements is alphanumerically by the package name. This should ignore whether the statement is ‘import’ or ‘from … import’.

Currently, ISort insists on separating ‘import’ versus ‘from … import’, resulting in statements that jumble the sequence of package names:

import apidiscing
import euismod
from dolor import consecteur
from ex import metus

Instead, ISort should order these statements by the package name:

import apidiscing
from dolor import consecteur
import euismod
from ex import metus

There does not seem to be any combination of ISort settings that will obey this sequence.

enhancement question

Most helpful comment

I think the default behavior is correct and usually the preferred one.

@timothycrosley do you think it's a possible candidate for enhancement to enable sorting _only_ by the package name? Possibly via some cmd line flag?

All 7 comments

I think the default behavior is correct and usually the preferred one.

@timothycrosley do you think it's a possible candidate for enhancement to enable sorting _only_ by the package name? Possibly via some cmd line flag?

Can you accomplish this force_sort_within_sections? If that does not work maybe you would also need force_alphabetical_sort. (isort settings wiki page)

$ isort --help
  ...
  -fas, --force-alphabetical-sort
                        Force all imports to be sorted as a single section
  -fass, --force-alphabetical-sort-within-sections
                        Force all imports to be sorted alphabetically within a
                        section
  ...
  -fss, --force-sort-within-sections
                        Force imports to be sorted by module, independent of
                        import_type
  ...

@benspaulding, the “sort by package name” example shown:

import apidiscing
from dolor import consecteur
import euismod
from ex import metus

Using the --force-sort-within-sections option retains the desired ordering. From the option description:

Force imports to be sorted by module, independent of import_type

@timothycrosley timothycrosley added the enhancement label 14 hours ago
@timothycrosley timothycrosley closed this 14 hours ago
@timothycrosley timothycrosley added the question label 14 hours ago

Adding the “enhancement” label makes sense, this is a request for enhancement.

What are we to understand from closing this request, without an implementation of the enhancement?

Hi @bignose-debian,

I've been trying to go through and categorize issues, and closing any that I thought where complete. When I saw your message above that --force-sort-within-sections retained the desired ordering I read that as the desired feature was already implemented with that flag and thus it was an enhancement request that turned into a question that had since been answered. Is my understanding of the issue incorrect?

Thanks!

~Timothy

When I saw your message above that --force-sort-within-sections retained the desired ordering I read that as the desired feature was already implemented with that flag […]

You're quite right, this request is already met by that option. This issue can be closed (and IMO the “question” label removed), sorry for the noise.

Hi @bignose-debian,

No worries! Thank you for the confirmation!
The question label is only there so I can filter through any areas of confusion and make a comprehensive FAQ document in the future.

Thanks!

~Timothy

Was this page helpful?
0 / 5 - 0 ratings

Related issues

johnthagen picture johnthagen  ·  3Comments

ghickman picture ghickman  ·  3Comments

pradyunsg picture pradyunsg  ·  3Comments

peteboothroyd picture peteboothroyd  ·  3Comments

darkclouder picture darkclouder  ·  3Comments