[Import-SIG] Loading Resources From a Python Module/Package

Erik Bray erik.m.bray at gmail.com
Mon Feb 2 23:34:59 CET 2015

Sorry for the late reply, but I'm still catching up on this...

On Fri, Jan 30, 2015 at 6:37 PM, Donald Stufft <donald at stufft.io> wrote:
> It's often times useful to be able to load a resource from a Python module or
> packaging. Currently you can load the data into memory using pkgutil.get_data
> however this doesn't help much if you need to pass that data into an API that
> only accepts a filepath. Currently code that needs to do this often times does
> something like os.path.join(os.path.dirname(__file__), "myfile.txt"), however
> that doesn't work from within a zip file.


> A. What do people think about pkgutil.get_data_filename and
>    Loader.get_data_filename?

Big +1 for me.  This deficiency has been an annoyance to me for some
time--just, I guess, not enough of an annoyance to propose any general
solution.  Astropy has some pretty hideous workarounds [1] to get
resource loading from zipfiles working, and even then it only works
for zipfile loaders and not other arbitrary loaders.  Fortunately
that's the only case I know if that matters to me (for use with
PyInstaller and other such software bundlers).

So I would love to see such an interface, and although one could argue
about the details I think the interface you proposed for
Loader.get_data_filename is fairly obvious and would have been my
first pass proposal as well.

> B. What do people think about modifying Loader.get_data so it can support
>    relative filenames instead of the calling code needing to handle that?

Yes, please.  Makes much more sense.


[1] https://github.com/embray/astropy/blob/issue-960/astropy/utils/data.py#L739

More information about the Import-SIG mailing list