On Tue, Nov 13, 2018 at 09:34:33PM -0800, Yuval Greenfield wrote: > I would like to propose allowing importing of strings that would support > relative paths. For example in Danish's example: > > # use this in `test_main.py` > import '../main.py' as main
How does that differ from existing syntax? from .. import main Off the top of my head, a few more questions that don't have obvious answers (at least not to me): What happens if main.py doesn't exist, but main.pyc does? What if you want to import from a sub-package, rather than a single-file module? What happens when Windows users use a backslash instead of a forward-slash? Does this syntax support arbitrary relative paths anywhere on the file system, or is it limited to only searching the current package? How does it interact with namespace packages? What happens if you call os.chdir() before calling this? Invariably people will want to write things like: path = '../spam.py' import path as spam (I know that's something I'd try.) What will happen there? If that is supported, invariably people will want to use pathlib.Path objects. Should that work? > Maybe the syntax can be improved, but to me this need has been aching since > I started using Python 12 years ago. I've used C, C++, and Javascript where > the whole "how do I connect these two files that are a folder apart" > problem doesn't require googling for documentation on packaging tools, > magic filenames, constraints and gotchas. The solution is always obvious > because it works just like it works in every system - with a file-relative > path. Beware of "obvious" solutions, because so often they lead to not so obvious problems. Like Javascript's "relative import hell": Quote: // what we want import reducer from 'reducer'; // what we don't want import reducer from '../../../reducer'; https://medium.com/@sherryhsu/how-to-change-relative-paths-to-absolute-paths-for-imports-32ba6cce18a5 And more here: https://goenning.net/2017/07/21/how-to-avoid-relative-path-hell-javascript-typescript-projects/ https://lostechies.com/derickbailey/2014/02/20/how-i-work-around-the-require-problem-in-nodejs/ It seems to me that in languages which support this file-relative import feature, people spend a lot of time either trying to avoid using it, or building tools to allow them to avoid using it. I don't know if that makes it better or worse than Python's solution for relative imports :-) -- Steve _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/