-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Can not load package data resources when running in a zipapp #2965
Comments
At least I guess that |
When installed in a Zip file ('zipapp', '*.pyz') loading resources from package data can not be done by directly reading files relative to '__file__', since there is no valid path on the file system for zipped files. Using 'pkgutil.get_data' ensures that loading files from a Zip archive is handled accordingly. GitHub: python-poetry#2965
When running from a Zip file ('zipapp', '*.pyz'), loading resources from package data can not be done by directly reading files relative to '__file__', since there is no valid path on the file system for zipped files. Using 'pkgutil.get_data' ensures that loading files from a Zip archive is handled accordingly. GitHub: python-poetry#2965
When running from a Zip file (`zipapp`, `*.pyz`), loading resources from package data can not be done by directly reading files relative to `__file__`, since there is no valid path on the file system for zipped files. Using `pkgutil.get_data` ensures that loading files from a Zip archive is handled accordingly. Resolved: python-poetry#2965
When running from a Zip file (`zipapp`, `*.pyz`), loading resources from package data can not be done by directly reading files relative to `__file__`, since there is no valid path on the file system for zipped files. Using `pkgutil.get_data` ensures that loading files from a Zip archive is handled accordingly. Resolves: python-poetry#2965
It is not possible to file issues against poetry-core, but basically there is a similar issue in poetry-core as well. Actually in one of its vendored dependencies, namely jsonschema:
The culprit lines seem to be the following in + with open(
+ os.path.join(os.path.dirname(__file__), "schemas", "{0}.json".format(name))
+ ) as f:
+ data = f.read()
- data = pkgutil.get_data("jsonschema", "schemas/{0}.json".format(name))
- return json.loads(data.decode("utf-8"))
+ return json.loads(data) Not sure why the patch has been written to replace the call to |
When running from a Zip file (`zipapp`, `*.pyz`), loading resources from package data can not be done by directly reading files relative to `__file__`, since there is no valid path on the file system for zipped files. Using `pkgutil.get_data` ensures that loading files from a Zip archive is handled accordingly. Resolves: python-poetry/poetry#2965
Next occurrence is in lark-parser: https://github.com/lark-parser/lark/blob/0.9.0/lark/lark.py#L347-L362
This would need to be patched (and sent upstream). |
On the lark front... |
When running from a Zip file (`zipapp`, `*.pyz`), loading resources from package data can not be done by directly reading files relative to `__file__`, since there is no valid path on the file system for zipped files. Using `pkgutil.get_data` ensures that loading files from a Zip archive is handled accordingly. Resolves: python-poetry/poetry#2965
When running from a Zip file (`zipapp`, `*.pyz`), loading resources from package data can not be done by directly reading files relative to `__file__`, since there is no valid path on the file system for zipped files. Using `pkgutil.get_data` ensures that loading files from a Zip archive is handled accordingly. Resolves: python-poetry/poetry#2965
I started with On the other hand there is Anyway, I would rule out setuptools' |
We just patched our own vendored copy of poetry for this with:
- licenses_file = Path(__file__).parent / "data" / "licenses.json"
+ licenses_file = importlib.resources.files("poetry.core.spdx").joinpath("data/licenses.json")
def validate_object(obj: dict[str, Any], schema_name: str) -> list[str]:
- schema_file = SCHEMA_DIR / f"{schema_name}.json"
-
- if not schema_file.exists():
- raise ValueError(f"Schema {schema_name} does not exist.")
+ schema_file = importlib.resources.files("poetry.core.json").joinpath(f"schemas/{schema_name}.json") While we're running on 3.10 & 3.11, |
(those patches appear to belong with the poetry-core repo rather than this one), also it appears the files APIs were introduced in 3.9 (also as noted in sinoroc's comment). An |
This is part of the fix for python-poetry/poetry#2965. Constructing a regression integration test that uses a zipapp like environment would be useful but is not part of this PR.
someone who understands the needs of the project should take over my PR. |
zipapp
)Issue
Culprit is probably this block of code:
Loading resources from package data should be done with
pkgutil.get_data()
(orimportlib-resources
orpkg_resources
), especially if the code is running directly from a zipped file. These libraries are able to handle zipped resources.References:
importlib-resources
is now much better since 1.3 and it seems to have been ported into Python 3.9'simportlib.resources
The text was updated successfully, but these errors were encountered: