Relative Content

Tag Archive for python-3.xpython-packagingpython-importlib

How to obtain path to a python data file using importlib without using a context manager?

My goal is to load datafiles from an installed Python package using the importlib package. To be fair, this package API seems very complex to use to me and many methods seem to be deprecated. I need to get the absolute path to the data file to provide as input to another program and not do IO directly on the data file.
From what I understood, if I have a package called foo and a subpackage called foodata then I can use a context manager to get a path to the file my_file.txt as follows: