On Wed, Nov 14, 2018 at 5:15 PM Steven D'Aprano <st...@pearwood.info> wrote:
> 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

Agreed. Having spent a lot of time with JavaScript students, I
actually am NOT a fan of directory-relative imports. They inevitably
result in equivalent code looking different, and subtly different code
looking identical. Consider:

// main.js
import User from './models/users';

// routers/users.js
import User from '../models/users';

That example is probably okay, because if you ever get it wrong, you
get an immediate error. But what about this?

// routers/index.js
import usersRouter from './users';

// models/stuff.js
import User from './users';

The exact same import does completely different things based on which
file it's in. That's dangerous, because it makes code subtly context
sensitive. I would much rather work with package-relative pathing,
where there is a known basis for *all* local imports, no matter what
file the import is actually happening in. In Python, that's best done
by naming the package again, so that's not quite ideal either, but
it's better than having to pile in the exact right number of "../" to
make the import work.

ChrisA
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to