Replace pkg_resources by importlib.resources#5039
Conversation
| from importlib.resources import as_file, files | ||
|
|
||
| with as_file(files("fontbakery").joinpath(sub_file_path)) as path: | ||
| file_path = path | ||
| return file_path |
There was a problem hiding this comment.
technically this is not correct, it will only work when fontbakery is loaded from a filesystem directory (e.g. when installed with pip). It's possible to import python modules from zip files as well, or tools can be used to embded them in a self-contained exe. To support use cases like this you should do what @m4rc1e did in
There was a problem hiding this comment.
https://docs.python.org/3/library/importlib.resources.html#importlib.resources.as_file
Exiting the context manager cleans up any temporary file or directory created when the resource was extracted from e.g. a zip file.
There was a problem hiding this comment.
if you want the temporary resource path to persist until python exits, you can do what Marc did in gftools tests/test_items.py in the PR above, i.e. use contextlib.ExitStack + atexit
There was a problem hiding this comment.
Thank you! I've updated the PR in #5045. I now return the file contents from the helper function, so no temporary files need to stick around.
Fixes #5028.