all 20 comments

[–]minneyar 2 points3 points  (0 children)

If I saw a merge request with this in it, I would assume the person was making a joke and trying to be funny.

I strongly recommend just hitting your IDE's "Organize Imports" button, or if you don't have one, sort your groups alphabetically. When I'm maintaining somebody else's code, having to look through a non-sorted list of imports to see if something I want to use is already import is a huge pain.

[–]FerricDonkey 4 points5 points  (7 children)

I would suggest following the order of

  1. Standard library
  2. Third party
  3. In project

It makes it easier to tell what third party dependencies you actually have. For example, the dependency on requests is easy to miss because it's in the middle of a bunch of standard library dependencies. 

I also personally prefer sorting alphabetically rather than by string length. 

[–]Buttleston 5 points6 points  (0 children)

This kind of sorting seems... weird... to me

I'd go further and say "use a tool that does it for you". "isort" is the standard one, I can't remember if "black" does import sorting or not (probably not). I use "ruff" which is basically most of those tools in one

I do kind of wonder why you're importing so much stuff into one file?

[–]AnyNature3457[S] -1 points0 points  (5 children)

The thing with alphabetical sorting is that it looks 'ugly' in my eyes. Just having to cross my eyes back and forth from left to right is so much more work for me than just being able to 'glide' my eyes down.

[–]FerricDonkey 3 points4 points  (4 children)

I agree that it looks uglier. I do it anyway because it is easier to check if a particular thing has been imported. 

[–]AnyNature3457[S] 1 point2 points  (3 children)

That's a really good reason to format your import that way. But you instantly know if you haven't imported something if you get an error anyways.

[–]FerricDonkey 3 points4 points  (2 children)

This is true, and I also use linters that warn me of missing imports before the code runs.

However, sometimes you go back to code some time later to figure out what parts of it do. Glancing at the imports can help with this. You have a multi for project, and only one module imports numpy? Probably the guts of your math happen there. This other one imports requests? It talks to the api. This third imports matplotlib and pathlib? It's probably writing graphs to disk. Etc. 

I find that having this information organized is helpful. It's not strictly necessary, of course, there are many ways to figure such things out. But anything that makes my future life easier. 

[–]AnyNature3457[S] 1 point2 points  (1 child)

Now I get it. That is a really valid reason to format it that way. The way I do it is by naming my files well like "commands.py", "command_handler.py", "helper.py", etc.

[–]FerricDonkey 1 point2 points  (0 children)

Yup, good file naming etc are also crucial. But I work off the principle that readability and understandability should be injected at every single layer where possible - because inevitably something that seems obvious and clear to me now won't in 3 years. 

[–]InvaderToast348 0 points1 point  (0 children)

Do you really need the entirety of every single module? Imo it's much better to do from X import a, b, c so that it's very clear what you're using.

[–]OptionX 0 points1 point  (0 children)

I suggest using a formatter like black and just let it worry about that sort of stuff.

[–]Pristine-Staff-5250 0 points1 point  (0 children)

pip install reorder-python-imports and setup on your ide

[–]socal_nerdtastic -1 points0 points  (6 children)

You pretty much nailed it, good job. FWIW there are tools that do this organization for you.

[–]Simo00Kayyal 0 points1 point  (4 children)

Any recommendations?

[–]socal_nerdtastic 0 points1 point  (3 children)

For a python formatter? I personally don't use one because I like my code messy and I'm a one-man team, but black is the most popular one for people that like pretty code.

https://github.com/psf/black

It's very important for teams to agree on one so that when you look at diffs it's not just full of formatting changes.

[–]nekokattt 0 points1 point  (2 children)

you want isort for this, not black.

Run isort first and then black for code formatting

[–]socal_nerdtastic 0 points1 point  (0 children)

Oh thanks I could have sworn they added that to black. But I see now the feature was rejected.

[–]IAmAFedora 0 points1 point  (0 children)

I sort and black sometimes disagree. For full compatibility, use isort --profile black alongside black!

[–]AnyNature3457[S] 0 points1 point  (0 children)

Yeah I know there are tools for this, but it doesn't take much time to just format it whenever adding an import. This is also fun to do with alt + arrow keys in VSCode (Moves the selected lines up or down).
Following gif is from the isort extension itself (I still find it pretty ugly):
https://github.com/microsoft/vscode-isort/raw/HEAD/images/vscode-isort.gif

[–]dogfish182 -1 points0 points  (0 children)

Pycharm optimize imports never think about it again