Skip to content

Replace pkg_resources by importlib.resources#5039

Merged
felipesanches merged 5 commits intomainfrom
remove-pkg_resources
Aug 27, 2025
Merged

Replace pkg_resources by importlib.resources#5039
felipesanches merged 5 commits intomainfrom
remove-pkg_resources

Conversation

@jenskutilek
Copy link
Copy Markdown
Contributor

Fixes #5028.

@felipesanches felipesanches merged commit d289f33 into main Aug 27, 2025
47 of 52 checks passed
Comment thread Lib/fontbakery/utils.py
Comment on lines +51 to +55
from importlib.resources import as_file, files

with as_file(files("fontbakery").joinpath(sub_file_path)) as path:
file_path = path
return file_path
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

googlefonts/gftools#1131

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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 this pull request may close these issues.

Deprecated pkg_resources API should be fixed before it stops working

4 participants