5 ms·
So much this. There's no good reason to use relative imports. They're less readable, more dangerous and don't solve a single problem.
by scaryclam 5y ago
So much this. There's no good reason to use relative imports. They're less readable, more dangerous and don't solve a single problem.
- zarzavat 5y agoIn JS land everything is a relative import. It's nice because you can move a whole directory of code from one project to another (or around in the same project) and it still works because all the imports pointing to other files inside that directory were relative, you only have to fix imports that go outside the directory. Also the way that JS imports are just relative paths is very nice because it means that the imports are statically determinable, your editor can understand them and fix them automatically and you can trust that refactoring. Python has turing complete imports because there's so much dynamic messing about with sys.path that goes on in Python due to inadequacies of the import system.
- noptd 5y agoGiven most editors handle absolute imports as good as if not better than relative imports, I don't see any real benefit to using the latter.
- true_religion 5y agoIt stops some circular imports if you use relative imports.
- np_tedious 5y agoI'd rather suffer the late import with absolutes than wrestle with relative imports
- BossingAround 5y ago> It's nice because you can move a whole directory of code from one project to another (or around in the same project) and it still works Yes, just like absolute imports... > relative paths is very nice because it means that the imports are statically determinable, your editor can understand them and fix them automatically and you can trust that refactoring 1) Just like absolute imports? 2) I absolutely contest that relative imports are easier for an IDE to refactor. I've never had VSCode hang while refactoring a Java package; I've had VSCode hang while refactoring "create-react-app" app...
- zarzavat 5y agoIn Python you can do (and people do do) this import a.b.c sys.path.insert(0, 'some/other/path') import x.y.z Or env PYTHONPATH="foo/bar:$PYTHONPATH" python ... There is no way in general for Python tooling to figure out where an import points. All it can do is guess. It's a very regrettable situation. Yes in Java it's fine because Java has a build stage. So tooling can figure out where imports point to from your static configuration. However from the tool's perspective there is nothing better than a relative path. Relative paths require no messing about with configuration files at all to resolve. It's just a path to another file or directory on disk. When a tool sees import c from "./a/b/c" It can resolve it immediately. So what is the advantage of absolute imports really? If import lines are mainly written and maintained by tooling then shouldn't we pick the representation that is easiest for the tooling? Then we can have more and better tools and the tools will be more reliable. And it turns out that, relative paths are easy for humans to understand too. The same configuration-free resolution algorithm also works in your head when you are reading code! At least when the language doesn't overcomplicate them too much (JS is guilty of this to a certain extent, although nowhere as bad as Python)
- hermitdev 5y agoIIRC, it was done for security, to avoid inadvertently picking up a library from an unexpected place. For instance, drop a file named test.py in the same directory as your python 2 script and have "fun" figuring out what went wrong.