Re: Loading Resources From a Python Module/Package
Donald Stufft <[email protected]> Sat, 31 Jan 2015 10:47:44 -0500
| Newsgroups | gmane.comp.python.import |
|---|---|
| Message-ID | <[email protected]> |
> On Jan 31, 2015, at 10:46 AM, Paul Moore <[email protected]> wrote: > > On 31 January 2015 at 15:34, Donald Stufft <[email protected]> wrote: >> It seems obvious to me that requiring a full path like that is the wrong way >> to expect people to work with constructing full paths for resources. It >> would be similar to expecting people to do ``import >> /data/foo.zip/submodule``. The import system should be abstracting all of >> that away for them. > > Note the example in PEP 302: > > d = os.path.dirname(__file__) > data = __loader__.get_data(os.path.join(d, "logo.gif")) > > The parallel is with the historical filesystem-only approach, > > d = os.path.dirname(__file__) > with open(os.path.join(d, "logo.gif"), 'rb') as f: > data = f.read() > > You *don't* want to use a relative pathname then in this case, so the > loader protocol is designed to follow that usage. As Brett says, > __file__ can have non-filesystem "token" elements (e.g., a zipfile > name) if necessary. > > It's certainly possible to add a new API that loads resources based on > a relative name, but you'd have to specify relative to *what*. > get_data explicitly ducks out of making that decision. data = __loader__.get_bytes(__name__, “logo.gif”) --- Donald Stufft PGP: 7C6B 7C5D 5E2B 6356 A926 F04F 6E3C BCE9 3372 DCFA _______________________________________________ Import-SIG mailing list [email protected] https://mail.python.org/mailman/listinfo/import-sig