2

I want to add a submodule for https://github.com/loonghao/photoshop-python-api, when I found all code in this repo uses absolute importing.
Well, I would follow his code style, if importing in my submodule didn't become something like this:

from photoshop.api.action_manager._main_types.action_descriptor_iterator import ActionDescriptor_Iterator

What's worse, his repo follows google guidelines, which says that everyone should make clear what a variable is for by naming it properly. That makes a line of importing easily exceeds the limit of 120 chars when using absolute importing.
What I think is that relative importing will cause error when you directly run any code in the module. But in this case no one would.
So: Is there any evidence indicating that we should always use absolute importing? Can absolute importing, which sacrefices portability and convenience, bring any convenience in maintaining when relative importing fits the situation?

23Xor
  • 64
  • 5
  • Maybe I am not saying it properly, but I don't blame google guidelines. – 23Xor Sep 04 '22 at 10:34
  • You say the repo follows the Google style guide. Absolute imports are *also* in the [Google style guide](https://google.github.io/styleguide/pyguide.html#22-imports): "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." – user2357112 Sep 04 '22 at 10:44
  • "What I think is that relative importing will cause error when you directly run any code in the module." I'm not sure what issue you have in mind. If you mean [this](https://stackoverflow.com/questions/11536764), well, see the answers. – Karl Knechtel Sep 04 '22 at 10:45
  • I don't know what the Google style guide authors are thinking, but "This helps prevent unintentionally importing a package twice." makes no sense to me. Imported modules (and, thus, packages, which are represented as `module` objects) are cached, and even if they weren't, I can't fathom why changing the import method would be relevant. – Karl Knechtel Sep 04 '22 at 10:46
  • 1
    Note: the Google style guide is pretty old, dating back to Python versions without explicit relative imports, and without the ability to turn off implicit relative imports. Implicit relative imports were a really bad thing back in the day. Preferring absolute imports everywhere made a lot of sense at the time. Now they've got a giant code base that's already using absolute imports everywhere, and changing the style isn't a decision to be made lightly. – user2357112 Sep 04 '22 at 10:49
  • This style decision was made back at a time when it wasn't a decision between `import spam.eggs` and `from . import eggs`. It was a decision between `import eggs` and `import spam.eggs`. You had no idea whether `eggs` was a top-level module, a sibling in a package hierarchy, an "uncle", or something else if your code base allowed relative imports. That kind of relative import was terrible, and there's a good reason they got removed. – user2357112 Sep 04 '22 at 10:54
  • @user2357112 I think your comments are worth writing up as an answer. The question can probably be fixed up to come across less subjective. – Karl Knechtel Sep 05 '22 at 04:53

1 Answers1

0

I've found my answer: not always.

As mentioned in a comment:

You say the repo follows the Google style guide. Absolute imports are also in the Google style guide: "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."

What I think: Guidelines are written to achieve their "goal". In this case, "prevent unintentionally importing a package twice".
And the "goal" of my using relative import is to shorten the import. If I don't shorten the import, there is a bigger possibility for me to mistype the path. In my case it would take much time to find where is wrong in those large path strings.

So now my "goal" brings more convenience in maintaining than google's "goal", and it's clear that I should choose relative importing.

23Xor
  • 64
  • 5
  • In the same theory, if there's a tool that can make writing these large path strings easier for me, for example, some ide can do auto completion in path for me, then google's goal can outweight my goal. – 23Xor Sep 04 '22 at 11:16