Avoid importing nx directly in submodules#13
Avoid importing nx directly in submodules#13stefanv wants to merge 1 commit intodschult:doc_classicfrom
Conversation
|
Interesting... Thanks for this.
One of the difficulties I've had learning about relative imports is that the code is no longer "stand alone". It changes depending on where in your package it is located. It means I have to learn a lot more about NetworkX than just one module before I feel comfortable contributing. There must be an advantage to having code depend on the module's location in the package. I understand that relative imports makes it easier to change the directory structure of the package. But 1) how often is that done? and 2) does it really save that much? It's an easy grep and replace to change the name |
|
I also don't understand the advantages of relative imports. Here is my current thinking (which could possibly be confused or wrong): Relative imports are succinctI don't have a problem with the longer versions. Our directory structure is not that deep. I think this is one of our longest imports It is pretty long, but we don't use it much. This seems like a small benefit for projects with deeper hierarchies. Relative imports makes changing things harderIf, for example, we move to But we would need to be careful not to change in functions inside of And we would also need to change all the imports inside of With absolute imports, we would just need to do one global search for and replace them with This seems like a pain that would likely lead to errors. Relative imports seem less easy to readIt requires paying more attention to the directory structure and remembering where you are in it. With absolute imports I always use the same import no matter which file I am editing. This seems like a negative. |
|
I think the absolute imports are fine, as long as you specify the whole path to modules. These are different syntaxes for the same thing. What should be avoided is doing: |
|
I am also unclear on the advantages / disadvantages of vs inside the library. Although, I prefer (though I don't know exactly why) More generally, my current (slight) preference would be to always do something like vs or or The only exceptions being things like if we need to (for example) use both centralities in the same module. But I don't have a strong sense for why other than it seems more explicit (and less likely to cause confusion) and more succinct. I am not as concerned about having to read the imports at the top of the file to see where things are coming from. So I don't think we need to do But I am not strongly opposed to this. |
|
Regardless of what we decide to do about the imports, we still need to figure out how to stop our circular imports. Currently gives us And gives us |
|
Now this will probably mess up the discussion completely, but: why are circular imports a problem? (they fill up namespace dict, but only with 1 extra item.. right? They don't get used. Any introspection code should be able to stop an infinite loop, right?) why should we avoid using Are these style concerns (which I can definitely understand) or is there a deeper rationale? I can probably get used to relative imports even just for style. But I'm more interested in understanding the advantages. |
This is how I would restructure imports to make use of relative imports. Feel free to use or not use as you see fit.