GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||
GeezerVison is showing github.com — Support non-package modules. by jaraco · Pull Request #258 · python/importlib_resources Conversation
Member
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
|
There was an error while loading. Please reload this page.
|
@warsaw I'd appreciate your review of this change, especially since you were involved so much in the discussion on #60. The behavior change is entirely in the second commit ecffc07. Note how it simplifies the code, aligns with |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
See jaraco/openpack@82a5d0b for an illustration of how this change simplifies and harmonizes resource loading. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| _path.build(spec, self.site_dir) | ||
| import mod | ||
|
|
||
| actual = resources.files(mod).joinpath('res.txt').read_text() |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
This is surprising to me. I would expect, a pathlib.Path('...', 'mod.py') to be returned, requiring resources.files(mod).parent.joinpath('res.txt'). res.txt is not a resource of mod, but rather of the parent module, which might not even exist if mod is top level.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Thanks for sharing your surprise. Surely this would need some improved documentation to illustrate how files() now accepts an Anchor (instead of Package), where the anchor indicates:
This is the behavior indicated by pkg_resources:
Note that if a module name is used, then the resource name is relative to the package immediately containing the named module.
However, I've also observed that the pkg_resources implementation also allows for loading resources against unpackaged modules. And while that behavior may have been unintentional, it works because it's a natural consequence of relying on the importer/loader to resolve the resources from the same location as the indicated package/module. That is, because the importlib mechanism supports loading modules from outside packages, so too should resources be loadable from that location.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
res.txtis not a resource ofmod, but rather of the parent module, which might not even exist ifmodis top level.
A parent module may not exist, but a loader for that module does exist and can load resources adjacent to that module:
importlib_resources feature/203-non-package-modules $ touch setup.py
importlib_resources feature/203-non-package-modules $ .tox/python/bin/python -iq
>>> import importlib_resources as res
>>> files = res.files('setup')
>>> files
PosixPath('/Users/jaraco/code/public/importlib_resources')
>>> files.joinpath('pyproject.toml').read_text()[:10]
'[build-sys'
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
In 1f3c226, I've updated the documentation to honor an Anchor type which need not be a package.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
…tion of resources from adjacent modules, even those not found in a package. Fixes #203.
1923197 to
1f3c226
Compare
1f3c226 to
8c3edeb
Compare
2ff7b03 to
4e63613
Compare
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
Unable to load resources for modules
There was an error while loading. Please reload this page.