First of all, yes I'm aware of PEP8 and the alphabetical method.

I do sort my imports first by the PEP8 recommendation:

  1. standard library imports
  2. related third party imports
  3. local application/library specific imports

But then, within each category, I like to sort by the length of the module name. Yes, it makes it look pretty, but then I find it arguably makes it easier to find a module than the alphabetical way.

Also, the shorter names tend to be the more basic, common modules (like os and sys), so there's an additional organization layer in that way. You know the length of the name you're looking for, and the visual shape of the import list tells you very quickly where to look for names that long.

An example:

import os
import sys
import time
import logging
import argparse
import subprocess

import django
import requests
import webencodings

import mymodule
import localthing
import supercustomstuff

Right now I mostly write for myself, and I don't work with a team. But I intend to in the future. So what I'm wondering is, does anyone else do this, does anyone else see the sense in it, or will everyone else think I'm nuts?

Edit: Separate subgroups in example, as suggested by Mark Ransom, in order not to distract from the core issue about the ordering.

  • 1
    How do you decide what to put first when 2 modules have the same name length and belong in the same category? I would certainly say that for your own stuff, if it helps you, do it. I would not want someone doing that on a project I work on, though. – user1118321 Feb 14 '18 at 3:04
  • @user1118321 Usually I put the more "basic" ones above the rarer or more specialized ones. Good to hear the feedback. I'd love to hear you expand on why in an answer. – Nick S Feb 14 '18 at 3:13
  • 1
    Sounds to me like another firm of technical procrastination – whatsisname Feb 14 '18 at 3:52
  • 1
    The obvious downside of using an unusual style is that tools cannot help you maintain that style. There are tools that sort Python imports alphabetically. Do you know of any tools that automatically create your preferred order? – Sebastian Redl Feb 14 '18 at 10:45
  • 1
    Just to quickly answer one more of your questions, you're not the only one, this is also my usual approach. Even though I don't have any reasoning behind this approach other than "it's aesthetically appealing". – 301_Moved_Permanently Feb 18 '18 at 16:25

First, I would say to do what works for you on your own projects. It’s not going to hurt anything and it helps you, so it’s a win!

For projects with others I would discuss it with the team. Often a team will have a style guide that they expect developers to follow. Other times they allow every developer to do their own thing. I would advise following what others on your team do.

Personally, your system seems arbitrary to me and quite odd. Others probably have systems they use for equally arbitrary reasons. I don’t feel like yours would help me in general. I have to wonder how much it helps when the module names are all close in length? At least with alphabetic (which is also not my favorite) I know to use the first 2 letters when two files start with the same letter.

Also, I’m not sure about Python but in some languages, if developers aren’t careful, import order can change the behavior of a program. (That shouldn’t be the case and it shouldn’t be relied on, but the reality is that it happens.)

| improve this answer | |

This is a classic XY question. You ask about one issue, but I will suggest you actually have a different issue!

You say sorting by length makes it easier to find a module. So apparently you spend a significant amount of effort scanning through the list of imports to see if a certain import is present. This indicates to me you have far to many imports in a single file. Each file have too many different responsibilities and dependencies. Apply some modularization and separation of concerns to your own code, so each file will have a manageable number of dependencies. You should be able to see all imports in a section at a glance so ordering shouldn't matter that much.

As for sorting by name length, this does not make a lot of sense. The length of a name does not carry any particular meaning. Module names are selected to be simple and descriptive, but the length does not necessarily correspond to how "basic" they are, even if it happens to be the case in the examples you chose.

If you want to sort by how basic the modules are (which is reasonable) then do that, and ignore the length of names.

| improve this answer | |
  • 1
    Though the example given by the OP do not appear to me as being too much... your reasonning is spot-on ! Identifying the problem is one thing but it's only the first step towards understanding it. – Newtopian Feb 15 '18 at 19:07
  • 1
    Well this is a pretty interesting thought. I'm not sure what's too many, but I'd say about a dozen is typical for my scripts. I don't think I spend much time scanning; the comment about ease of scanning is mainly a preemptive response to those arguing for alphabetical. And the "basic"ness isn't the main motivation, but mainly a nice side effect. In any case, if you have advice on modularization to point to, I'd love to read it. – Nick S Feb 16 '18 at 7:09

Your ordering is weird. If you intend to work with other people, don't do it. Even if you're working on your own private project, don't allow yourself to get into bad habits.

I'd also put a blank line between the groups to separate them.

| improve this answer | |

It's indeed you own style and I see no harm in keeping it.

But, I'd say its rooted in a deeper instinct of a "natural order", or in fact a combination of various such orders:

  1. Common imports that are almost in every file always go in first. This is not surprising as the imports section slowly becomes a template in your head. A quick glance and you know what's "special" imported here.

  2. More specific things usually tend to have longer names, namespaces and import paths. So, the "generic first and specialized later" rule actually ends up with the length based ordering.

  3. The urge to group, categorize and separately mark everything is also pretty common. Closely related imports are usually grouped together.

  4. And finally, we cannot ignore the "visual aesthetics" of text, many programmers regard highly. We are arranging the information for our own quick processing.

| improve this answer | |

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.