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

Package resources with pkgutil, pkg_resources and importlib.resources #649

Open
thijsmie opened this issue Oct 14, 2022 · 3 comments
Open

Comments

@thijsmie
Copy link

I've tested several mechanisms for package resources with in-memory PyOxidizer builds and the results are inconsistent. I made a little demo repository where I try out different mechanisms: https://github.com/thijsmie/pyoxidizer-resource-tests I hope this can serve as a reproducer while debugging this.

A short summary would be that pkgutil never works, importlib.resources works in some cases and pkg_resources always works in my tests so far. This is probably related to #529.

@kambe-mikb
Copy link

I got importlib.resources to work, at least for my use case (loading translation files), but I found that it's broken on anything less than Python 3.10. The package does not work as advertised on 3.8 or 3.9 - it can only handle resources in a file system.

@thijsmie
Copy link
Author

thijsmie commented Nov 8, 2022

Indeed, I've also verified that on 3.9 it is not working at all. Sorry for tagging you @indygreg but can you comment on this? I would be happy to contribute a fix but I need a place to start...

@kambe-mikb
Copy link

I was probably too terse in my earlier comment. The problem, at least in the case of importlib.resources is not with PyOxidizer, or any of its moving parts. Before Python 3.10, the code in the importlib.resources consisted of stubs which just handled resource requests as calls to the file system, and didn't utilize the modern importlib machinery. This means that the machinery PyOxidizer installs to handle resource requests in in-memory modules is simply ignored. Python 3.10 and later now access the module spec to locate and open resources, which means resources in an in-memory package can be found.

My code uses importlib.resources.is_resource() and importlib.resources.open_binary(), and only runs properly with at least 3.10.

Because it's a Python problem, and the fix is "upgrade to current Python", I would suggest this can be closed.

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