Re: making it feasible to rely on loaders for reading intra-package data files
Barry Warsaw <[email protected]> Mon, 3 Feb 2014 16:00:36 -0500
| Newsgroups | gmane.comp.python.import |
|---|---|
| Organization | The Organization of Unorganized Woozalists |
| Message-ID | <[email protected]> |
On Feb 01, 2014, at 01:44 PM, Brett Cannon wrote: >Over on distutils-sig it came up that getting people to not simply assume >that __file__ points to an actual file and thus avoid using open() directly >to read intra-package files is an issue. I've always recommended that people use the Resource Manager APIs of pkg_resources to get at in-package data[*]. Those have always been the most reliable APIs AFAICT, but it's a shame that they're not available in the stdlib in any kind of backward compatible way. Maybe the breadth or implementation of pkg_resources prevents it from being adopted wholesale into stdlib (and of course, it's too late for 3.4), but I really think we need something like that which we can promote loud and far. And then there's PEP 365. pkgutil.get_data() is as close as the stdlib comes I think, but it's not enough since sometimes you actually need a file name, or some of the other pkg_resources APIs. -Barry [*] Specifically: resource_exists(), resource_stream(), resource_string(), resource_isdir(), resource_listdir().