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

A probable bug with zip imports #169

Closed
hukkin opened this issue Jan 31, 2025 · 3 comments · Fixed by #170
Closed

A probable bug with zip imports #169

hukkin opened this issue Jan 31, 2025 · 3 comments · Fixed by #170

Comments

@hukkin
Copy link
Contributor

hukkin commented Jan 31, 2025

I haven't confirmed, but seems like there might be an issue with zip imports here:

with as_file(files("em").joinpath("emojis.json")) as em_json:
EMOJI_PATH = em_json
CUSTOM_EMOJI_PATH = os.path.join(os.path.expanduser("~/.emojis.json"))

The as_file context manager, when exiting, according to the docs

cleans up any temporary file or directory created when the resource was extracted from e.g. a zip file.

So importlib.resources reads the file from the .zip, creates a temporary file, and deletes it when exiting, after which it can no longer be read.

I think best would be to call read_text or open on the importlib.resources.abc.Traversable and never make a pathlib.Path out of it.

@hukkin
Copy link
Contributor Author

hukkin commented Feb 1, 2025

I confirmed this is an issue, by importing and running the package as shown here https://docs.python.org/3/library/zipimport.html#examples

@hugovk
Copy link
Owner

hugovk commented Feb 4, 2025

Please can you give concrete steps to reproduce, including how to create the zip to begin with?

@hukkin
Copy link
Contributor Author

hukkin commented Feb 4, 2025

Yes, of course. One easy way to reproduce this is to use a regular .whl as the zip file and add that to PYTHONPATH:

pipx run build
PYTHONPATH=dist/em_keyboard-5.0.1.dev3-py3-none-any.whl python3.13 -m em grinning_face

This will currently error in main branch with

FileNotFoundError: [Errno 2] No such file or directory: '/tmp/tmp5yun71gaemojis.json'

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

Successfully merging a pull request may close this issue.

2 participants