Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
winstontsai opened this issue Jul 13, 2024 · 6 comments
Closed

Comments

@winstontsai
Copy link

winstontsai commented Jul 13, 2024

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.

@wimglenn
Copy link
Owner

wimglenn commented Jul 14, 2024

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?

@winstontsai
Copy link
Author

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
Copy link
Author

winstontsai commented Jul 14, 2024

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

@wimglenn
Copy link
Owner

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

@winstontsai
Copy link
Author

Created issue python/cpython#121735

Gatsik added a commit to Gatsik/client that referenced this issue Aug 4, 2024
this version was in the recent 0.21.0 release and worked fine

the irc library is broken (for us) since jaraco/irc@c95c063
the reason is that it uses importlib.resources.files() to import codes
and this function is buggy -- it doesn't work with freezed code
(see also python/cpython#121735. wimglenn/resources-example#6)
and (what a coincidence!) broken implementation of files() function was written
by the same person -- see python/cpython#100598
@wimglenn
Copy link
Owner

In

Resist the temptation to use importlib.resources.files() without any arguments, because
omitting the anchor currently will break zipapp packaging.
https://github.com/python/cpython/issues/121735
I've added a note advising to avoid this usage until the zipapp issue is fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants