[Python-ideas] pathlib.Path should handle Pythonpath or package root

Nick Coghlan ncoghlan at gmail.com
Wed Jul 19 10:53:23 EDT 2017


On 19 July 2017 at 22:01, George Fischhof <george at fischhof.hu> wrote:
> Sorry it was misunderstandable.
> I do not want to touch the import.
>
> I just would like to access the package root (of my current script).
> Practically in a Path object if the package resides in the file system.
> Parent is not enough, because the place of actual running script can vary.

If you just want to do relative imports, then that's the purpose of
the explicit relative import syntax ("from . import sibling" etc). You
can also do dynamic relative imports by passing "__package__" as the
second argument to importlib.import_module.

If you're looking to read *data* files relative to the current module,
then the API you're likely after is `pkgutil.get_data`:
https://docs.python.org/3/library/pkgutil.html#pkgutil.get_data

And if you're looking to access files relative to *__main__*, then the
metadata atribute you want is __main__.__file__.

So it's currently still really unclear what you actually mean by
"package root", and what related metadata you see as currently being
missing at runtime.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list