Mailing List Archive

CWD + relative path + import name == resultant relative path?
Is there a predefined function somewhere that can accept the 3 things on
the LHS in the subject line, and give back a resultant relative path -
relative to the unchanged CWD?

To illustrate, imagine:
1) You're sitting in /home/foo/coolprog
2) You look up the AST for /home/foo/coolprog/a/b/c.py
3) /home/foo/coolprog/a/b/c.py has a relative import of ..blah

Is there something preexisting that can figure out what path blah is
supposed to have, assuming 100% pure python code?

Or is it necessary to actually import to get that? I think I could import
and then check __file__, but I'd rather avoid that if possible.

Thanks!
--
https://mail.python.org/mailman/listinfo/python-list
Re: CWD + relative path + import name == resultant relative path? [ In reply to ]
Dan Stromberg wrote at 2021-10-29 16:38 -0700:
>Is there a predefined function somewhere that can accept the 3 things on
>the LHS in the subject line, and give back a resultant relative path -
>relative to the unchanged CWD?
>
>To illustrate, imagine:
>1) You're sitting in /home/foo/coolprog
>2) You look up the AST for /home/foo/coolprog/a/b/c.py
>3) /home/foo/coolprog/a/b/c.py has a relative import of ..blah

Look at the functions provided by `os.path`.

Maybe, you do not need relative path: look at `importlib`
(which also supports import via absolute paths).
--
https://mail.python.org/mailman/listinfo/python-list