resources-example icon indicating copy to clipboard operation
resources-example copied to clipboard

Add example of using importlib.resources.files() in Python 3.12

Open winstontsai opened this issue 1 year ago • 5 comments

This was a helpful example repo. I just have one request.

In Python 3.12 they updated importlib.resources.files(): https://docs.python.org/3/library/importlib.resources.html#importlib.resources.files

Changed in version 3.12: package parameter was renamed to anchor. anchor can now be a non-package module and if omitted will default to the caller’s module. package is still accepted for compatibility but will raise a DeprecationWarning. Consider passing the anchor positionally or using importlib_resources >= 5.10 for a compatible interface on older Pythons.

Could you include an example of omitting anchor so that it defaults to the caller’s module (or explicitly passing the module name with a string literal or __name__? I tried myself and while it works before packaging with zipapp, it fails to open and read the data file when running the zip. I'm not sure if I'm doing something wrong.

winstontsai avatar Jul 13 '24 21:07 winstontsai

It looks like this new feature is buggy. I get FileNotFoundError("Can't open orphan path") when trying to use that technique within a zipapp. Are you seeing the same error message?

wimglenn avatar Jul 14 '24 02:07 wimglenn

Yes I see that too. I wasn't sure if it was just buggy since it's a relatively new feature or if I didn't understand something.

winstontsai avatar Jul 14 '24 03:07 winstontsai

If you also think this is a bug, I will file an issue with cpython.

winstontsai avatar Jul 14 '24 03:07 winstontsai

It’s either a bug or a footgun that ought to be documented.

wimglenn avatar Jul 14 '24 04:07 wimglenn

Created issue https://github.com/python/cpython/issues/121735

winstontsai avatar Jul 14 '24 07:07 winstontsai

In https://github.com/wimglenn/resources-example/blob/8384ded77a3711155b2b3508f3f473207fffddcb/myapp/example5.py#L8-L10 I've added a note advising to avoid this usage until the zipapp issue is fixed.

wimglenn avatar Aug 14 '24 02:08 wimglenn