Replace pkg_resources with importlib.resources#65
Conversation
Co-authored-by: inducer <352067+inducer@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Updates add_py_module() to stop using deprecated pkg_resources and instead use the standard-library importlib.resources to locate a Python package’s bundled include/ directory for toolchain header search paths.
Changes:
- Replace
pkg_resources.resource_filename()lookup withimportlib.resources.files()for package include path resolution.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| from importlib.resources import files | ||
| return str(files(name) / "include") | ||
|
|
There was a problem hiding this comment.
importlib.resources.files() returns a Traversable that is not guaranteed to be a real filesystem path (e.g., zip/egg installs). Converting it to str and passing it as a compiler -I include dir can yield a non-existent path and break compilation, whereas pkg_resources.resource_filename() previously extracted resources to a real path. Consider resolving the package location via importlib.util.find_spec(...).submodule_search_locations (filesystem-only, like add_numpy) or materializing the directory with importlib.resources.as_file() and ensuring the extracted path remains valid for the entire build.
| from importlib.resources import files | |
| return str(files(name) / "include") | |
| from importlib.util import find_spec | |
| spec = find_spec(name) | |
| if spec is None: | |
| raise RuntimeError(f"Could not find module '{name}'") | |
| libdir = spec.submodule_search_locations | |
| assert libdir is not None | |
| from os.path import join | |
| return join(libdir[0], "include") |
Replaces deprecated
pkg_resourcesAPI with standard libraryimportlib.resourcesinadd_py_module().Changes
pkg_resources.resource_filename()withimportlib.resources.files()Behavior is equivalent for all current usages (codepy, pyublas, hedge) where distribution name matches module name. Compatible with Python 3.10+ requirement (
files()introduced in 3.9).Original prompt
pkg_resources#64💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.